2015-04-01 09:42:53 -06:00
#!/usr/bin/perl
2015-04-11 21:33:13 -06:00
=for comment
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (c) 2015 Darryl Quinn
See Contributors file for additional contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
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, see <http://www.gnu.org/licenses/>.
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
2016-12-23 22:23:58 -07:00
You must not misrepresent the origin of the material contained within.
2015-04-11 21:33:13 -06:00
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version.
=cut
2015-04-09 09:53:45 -06:00
$debug = 0;
2015-04-01 09:42:53 -06:00
BEGIN {push @INC, '/www/cgi-bin'};
use perlfunc;
use ucifunc;
use tunfunc;
2016-08-23 10:46:33 -06:00
$VPNVER="1.1";
2015-04-01 09:42:53 -06:00
$config = nvram_get("config");
$node = nvram_get("node");
$node = "NOCALL" if $node eq "";
read_postdata();
2018-05-02 18:42:04 -06:00
#################################
# save clients from form to UCI
#################################
sub save_clients()
{
my $enabled_count=0;
for ($i=0; $i < $parms{"client_num"}; $i++) {
my $net = $parms{"client${i}_netip"};
$rc=&uci_add_named_section("vtun","client_$i","client");
# generate the clientip and serverip
my ($clientip, $serverip) = &generate_ips($net);
$rc=&uci_set_named_option("vtun","client_$i","netip",$net);
push(@cli_err,"Problem saving UCI vtun client net IP (#$i): $rc") if $rc;
$rc=&uci_set_named_option("vtun","client_$i","enabled",$parms{"client${i}_enabled"});
2020-03-13 15:40:14 -06:00
push(@cli_err,"Problem saving UCI vtun client enabled (#$i): $rc") if $rc;
2018-05-02 18:42:04 -06:00
$rc=&uci_set_named_option("vtun","client_$i","name",$parms{"client${i}_name"});
2020-03-13 15:40:14 -06:00
push(@cli_err,"Problem saving UCI vtun client name (#$i): $rc") if $rc;
$rc=&uci_set_named_option("vtun","client_$i","contact",$parms{"client${i}_contact"});
push(@cli_err,"Problem saving UCI vtun client contact (#$i): $rc") if $rc;
2018-05-02 18:42:04 -06:00
$rc=&uci_set_named_option("vtun","client_$i","passwd",$parms{"client${i}_passwd"});
2020-03-13 15:40:14 -06:00
push(@cli_err,"Problem saving UCI vtun client password (#$i): $rc") if $rc;
2018-05-02 18:42:04 -06:00
# generate the VTUN NODE name based on the node name and netip
$net=~ s/\./\-/g;
my $vtun_node_name=$parms{"client${i}_name"} . "-" . $net;
$rc=&uci_set_named_option("vtun","client_$i","clientip",$clientip);
push(@cli_err,"Problem saving UCI vtun client client IP (#$i): $rc") if $rc;
$rc=&uci_set_named_option("vtun","client_$i","serverip",$serverip);
push(@cli_err,"Problem saving UCI vtun client server IP (#$i): $rc") if $rc;
$rc=&uci_set_named_option("vtun","client_$i","node",$vtun_node_name);
push(@cli_err,"Problem saving UCI vtun client name (#$i): $rc") if $rc;
$enabled_count++ if $parms{"client${i}_enabled"};
}
2021-04-23 08:46:09 -06:00
my $maxclients = &get_tunnel_maxclients();
push(@cli_err,"Number of clients enabled ($enabled_count) exceeds maxclients ($maxclients); only the first $enabled_count will activate.") if $enabled_count > $maxclients;
2018-05-02 18:42:04 -06:00
}
#################################
# save network info to UCI
#################################
sub save_network()
{
push(@cli_err,"The third octet of the network MUST be from 0 to 255") unless (($parms{server_net1}>=0) && ($parms{server_net1}<=255) && ($parms{server_net1} ne ''));
push(@cli_err,"The last octet of the network MUST be from 0 to 255") unless (($parms{server_net2}>=0) && ($parms{server_net2}<=255) && ($parms{server_net2} ne ''));
push(@cli_err,"The last octet of the network MUST be a multiple of 4 (ie. 0,4,8,12,16,...)") if ($parms{server_net2} % 4);
push(@cli_err,"Not a valid DNS name") unless (validate_fqdn($parms{dns}));
if (not @cli_err)
{
my $net=sprintf("%d.%d.%d.%d",172,31,$parms{server_net1},$parms{server_net2});
push @cli_err, "Problem saving the server network values!" if (&uci_set_indexed_option("vtun","network",0,"start",$net));
push @cli_err, "Problem saving the server DNS name!" if (&uci_set_indexed_option("vtun","network",0,"dns",$dns));
}
}
2015-04-01 09:42:53 -06:00
#################
# page checks
#################
if($parms{button_reboot})
{
system "/sbin/reboot";
}
if($parms{button_install})
{
2015-04-07 23:15:36 -06:00
install_vtun();
2015-04-01 09:42:53 -06:00
}
2015-04-02 10:52:46 -06:00
reboot_required() if($config eq "" or -e "/tmp/reboot-required");
2015-04-11 21:33:13 -06:00
&vpn_setup_required("vpn") unless(-e "/usr/sbin/vtund" );
2015-04-01 09:42:53 -06:00
#################
2015-04-08 18:04:43 -06:00
# If RESET, revert the UCI file
2015-04-01 09:42:53 -06:00
#################
if($parms{button_reset})
{
2015-04-08 18:04:43 -06:00
($rc,$res)=&uci_revert("vtun");
2015-04-09 09:14:04 -06:00
($rc,$res)=&uci_delete_option("vtun","network",0,"start");
2015-05-06 21:01:28 -06:00
($rc,$res)=&uci_delete_option("vtun","network",0,"dns");
2015-04-09 09:14:04 -06:00
$rc=&uci_commit("vtun");
2015-04-01 09:42:53 -06:00
}
#################
# get vtun network address
#################
@netw = ();
2015-04-02 10:52:46 -06:00
@netw = get_server_network_address();
2015-05-06 21:01:28 -06:00
$dns = get_server_dns();
2015-04-01 09:42:53 -06:00
#################
# If RESET or FIRST TIME, load clients/servers from file into parms
#################
if($parms{button_reset} or not $parms{reload})
{
2015-04-09 09:14:04 -06:00
# revert to previous state on initial load
($rc,$res)=&uci_revert("vtun");
2015-04-08 18:04:43 -06:00
# load clients from UCI
2015-04-01 09:42:53 -06:00
&get_client_info();
2015-05-07 21:55:15 -06:00
$parms{server_net1}=$netw[2];
$parms{server_net2}=$netw[3];
2015-05-06 21:01:28 -06:00
$parms{dns}=$dns;
2015-04-01 09:42:53 -06:00
# initialize the "add" entries to clear them
2015-04-08 18:04:43 -06:00
foreach $var (qw(client_add_enabled client_add_name client_add_passwd))
2015-04-01 09:42:53 -06:00
{
$parms{$var} = "";
2015-04-08 18:04:43 -06:00
$parms{$var} = "0" if($var eq 'client_add_enabled');
2015-04-01 09:42:53 -06:00
}
}
#################
# load clients from FORM and validate
#################
2015-04-08 18:04:43 -06:00
for($i =0 , @list = (); $i < $parms{client_num}; $i++) { push @list, $i }
2015-04-01 09:42:53 -06:00
push @list, "_add";
$client_num = 0;
foreach $val (@list)
{
2020-03-13 15:40:14 -06:00
foreach $var (qw(enabled name passwd netip contact))
2015-04-01 09:42:53 -06:00
{
$varname = "client${val}_$var";
2015-04-08 18:04:43 -06:00
$parms{$varname} = "0" if($val eq "enabled" and $parms{$varname} eq "");
2015-04-01 09:42:53 -06:00
$parms{$varname} = "" unless $parms{$varname};
$parms{$varname} =~ s/^\s+//;
$parms{$varname} =~ s/\s+$//;
if($val ne "_add")
{
2015-04-08 18:04:43 -06:00
if($parms{$varname} eq "" and ($var eq "enabled"))
2015-04-01 09:42:53 -06:00
{
$parms{$varname} = "0";
}
}
eval sprintf("\$%s = \$parms{%s}", $var, $varname);
}
# Validate ADDed values
if($val eq "_add")
{
# skip any null values on add or save
2020-03-13 15:40:14 -06:00
next unless ($enabled or $name or $passwd or $contact) and ($parms{client_add} or $parms{button_save});
2015-04-01 09:42:53 -06:00
} # no delete capabilities as net renumbering is not allowed
2018-05-02 18:42:04 -06:00
if($val eq "_add" and $parms{button_save})
2015-04-01 09:42:53 -06:00
{
push @cli_err, "$val this client must be added or cleared out before saving changes";
next;
}
2015-04-09 09:49:54 -06:00
# password MUST be alphanumeric (no special chars)
2015-04-09 09:57:57 -06:00
push @cli_err, "The password cannot contain non-alphanumeric characters (#$client_num)" if ($passwd =~ m/[^a-zA-Z0-9@]/);
2015-07-06 22:32:41 -06:00
push @cli_err, "The password must contain at least one alphabetic character (#$client_num)" if ($passwd !~ /\D/);
2015-04-01 09:42:53 -06:00
push @cli_err, "A client name is required" if($name eq "");
2015-04-08 18:04:43 -06:00
push @cli_err, "A client password is required" if($passwd eq "");
2015-04-01 09:42:53 -06:00
next if $val eq "_add" and @cli_err and $cli_err[-1] =~ /^$val /;
2015-04-08 18:04:43 -06:00
$parms{"client${client_num}_enabled"} = $enabled;
2015-04-01 09:42:53 -06:00
$parms{"client${client_num}_name"} = uc $name;
2015-04-08 18:04:43 -06:00
$parms{"client${client_num}_passwd"} = $passwd;
2015-04-20 22:53:57 -06:00
$parms{"client${client_num}_netip"} = $netip;
2015-04-01 09:42:53 -06:00
2015-04-08 18:04:43 -06:00
# Commit the data for this client
$client_num++;
2015-04-01 09:42:53 -06:00
# Clear out the ADD values
if($val eq "_add")
{
2020-03-13 15:40:14 -06:00
foreach $var (qw(net enabled name passwd netip contact))
2015-04-01 09:42:53 -06:00
{
$parms{"client_add_${var}"} = "";
}
}
}
2015-04-08 20:17:34 -06:00
$parms{client_num} = $client_num;
2015-04-01 09:42:53 -06:00
#################
2015-05-06 21:01:28 -06:00
# SAVE the server network numbers and dns into the UCI
2015-04-01 09:42:53 -06:00
#################
2015-04-09 09:14:04 -06:00
$netw[2]=$parms{server_net1};
$netw[3]=$parms{server_net2};
2015-05-06 21:01:28 -06:00
$dns=$parms{dns};
2015-04-08 20:17:34 -06:00
$rc=save_network();
2015-04-01 09:42:53 -06:00
2015-04-11 21:33:13 -06:00
#################
# SAVE the clients
#################
$rc=save_clients();
2015-04-01 09:42:53 -06:00
#################
2015-04-08 20:17:34 -06:00
# save configuration (commit)
2015-04-01 09:42:53 -06:00
#################
2015-05-08 22:26:54 -06:00
if($parms{button_save} and not @cli_err)
2015-04-01 09:42:53 -06:00
{
2015-04-08 20:17:34 -06:00
if (&uci_commit("vtun"))
{
push(@errors,"Problem committing UCI vtun");
}
2015-06-10 23:40:14 -06:00
&uci_clone("vtun");
2015-04-01 09:42:53 -06:00
unless($debug == 3)
{
2015-04-30 00:02:29 -06:00
# Regenerate olsrd files and restart olsrd
push(@errors,"Problem restarting olsrd") if system "/etc/init.d/olsrd restart > /dev/null 2>&1";
2015-04-08 20:17:34 -06:00
push(@errors,"Problem restaring vtundsrv") if system "/etc/init.d/vtundsrv restart > /dev/null 2>&1";
2015-06-14 22:30:06 -06:00
# delay to allow clients to connect and have an accurate "cloud" status
sleep 5;
2015-04-01 09:42:53 -06:00
}
}
2015-06-14 22:30:06 -06:00
@active_tun=&get_active_tun();
2015-04-01 09:42:53 -06:00
######################################################################################
# generate the page
######################################################################################
http_header() unless $debug == 2;
html_header("$node setup", 1);
print "<body><center>\n";
2015-05-24 14:08:19 -06:00
alert_banner();
2015-04-09 09:14:04 -06:00
print "<form id=vpn method=post action=/cgi-bin/vpn enctype='multipart/form-data'>\n" unless $debug == 2;
2015-04-01 09:42:53 -06:00
print "<form method=post action=test>\n" if $debug == 2;
print "<table width=790>\n";
#################
# Navigation bar
#################
print "<tr><td>\n";
navbar("vpn");
print "</td></tr>\n";
#################
# control buttons
#################
print "<tr><td align=center>";
print "<a href='/help.html#vpn' target='_blank'>Help</a>";
print " \n";
print "<input type=submit name=button_save value='Save Changes' title='Save and use these settings now (takes about 20 seconds)'> \n";
print "<input type=submit name=button_reset value='Reset Values' title='Revert to the last saved settings'> \n";
print "<input type=submit name=button_refresh value='Refresh' title='Refresh this page'> \n";
print "<tr><td> </td></tr>\n";
push @hidden, "<input type=hidden name=reload value=1></td></tr>";
#################
# messages
#################
if(@cli_err)
{
print "<tr><td align=center><b>ERROR:<br>";
foreach(@cli_err) { print "$_<br>" }
print "</b></td></tr>\n";
}
if($parms{button_save})
{
if(@cli_err)
{
print "<tr><td align=center><b>Configuration NOT saved!</b></td></tr>\n";
2015-04-11 21:33:13 -06:00
#}
#elsif(@errors)
#{
#print "<tr><td align=center><b>Configuration saved, however:<br>";
2015-04-01 09:42:53 -06:00
foreach(@errors) { print "$_<br>" }
print "</b></td></tr>\n";
}
else
{
print "<tr><td align=center><b>Configuration saved and is now active.</b></td></tr>\n";
}
print "<tr><td> </td></tr>\n";
}
#################
# everything else
#################
if($config eq "mesh")
2015-04-08 18:04:43 -06:00
{
2015-04-01 09:42:53 -06:00
print "<tr><td align=center valign=top>\n";
&print_vpn_clients();
2015-04-08 18:04:43 -06:00
print "</td></tr>\n";
2015-04-01 09:42:53 -06:00
print "<tr><td><hr></td></tr>\n";
}
print "</table>\n";
2015-04-08 18:04:43 -06:00
print "<p style='font-size:8px'>Tunnel v${VPNVER}</p>";
2015-04-01 09:42:53 -06:00
push @hidden, "<input type=hidden name=client_num value=$parms{client_num}>";
#################
# add hidden form fields
#################
foreach(@hidden) { print "$_\n" }
#################
# close the form
#################
print "</form></center>\n";
show_debug_info();
#################
# close the html
#################
2015-05-10 15:38:03 -06:00
page_footer();
2015-04-01 09:42:53 -06:00
print "</body></html>\n";
exit;
##################
# page subsections
##################
######################################################
2015-05-06 21:01:28 -06:00
# List the clients allowed to connect to this server
2015-04-01 09:42:53 -06:00
######################################################
sub print_vpn_clients()
{
2015-04-08 20:17:34 -06:00
print "<table cellpadding=0 cellspacing=0>";
2015-04-01 09:42:53 -06:00
2015-04-08 18:04:43 -06:00
print "<br /><tr class=tun_network_row><td colspan=6 align=center valign=top>Tunnel Server Network: ";
2015-05-07 21:55:15 -06:00
printf("%d.%d.",$netw[0],$netw[1]);
2016-08-23 10:46:33 -06:00
print "<input type='text' name='server_net1' size='3' maxlen='3' value='$netw[2]' onChange='form.submit()' title='from 0-255' >";
2015-04-01 09:42:53 -06:00
print ".";
2016-08-23 10:46:33 -06:00
print "<input type='text' name='server_net2' size='3' maxlen='3' value='$netw[3]' onChange='form.submit()' title='from 0-255 in multiples of 4. (ie. 0,4,8,12,16...252)' >";
2015-05-06 21:01:28 -06:00
print "<br /><hr>Tunnel Server DNS Name: ";
print "<input type='text' name='dns' size='30' value='$dns' onChange='form.submit()' ></td></tr>";
2015-04-08 20:17:34 -06:00
print "</table>";
2020-03-13 15:40:14 -06:00
#print "<hr />";
print "<table cellpadding=0 cellspacing=0>";
2015-04-08 20:17:34 -06:00
print "<tr><th colspan=6 align=center valign=top> </th></tr>\n";
print "<tr class=tun_client_row>";
2015-04-01 09:42:53 -06:00
print "<tr><th colspan=6>Allow the following clients to connect to this server:</th></tr>\n";
2020-03-13 15:40:14 -06:00
print "<tr><th colspan=6><hr></th></tr>\n";
2015-04-01 09:42:53 -06:00
print "<tr><th>Enabled?</th><th>Client</th><th>Pwd</th><th>Net</th><th>Active </td><th>Action</th></tr>\n";
2015-04-08 18:04:43 -06:00
for($i = 0, @list = (); $i < $parms{client_num}; ++$i) { push @list, $i };
2015-04-01 09:42:53 -06:00
2021-04-23 08:46:09 -06:00
push @list, "_add" unless($parms{client_num} >= &get_tunnel_maxclients());
2015-04-01 09:42:53 -06:00
$cnum=0;
foreach $val (@list)
{
2020-03-13 15:40:14 -06:00
foreach $var (qw(enabled name passwd contact))
2015-04-01 09:42:53 -06:00
{
eval sprintf("\$%s = \$parms{client%s_%s}", $var, $val, $var);
}
2015-04-08 18:04:43 -06:00
print "<tr class=tun_client_add_row><td height=10></td></tr>\n" if $val eq "_add" and scalar(@list) > 1;
2020-03-13 15:40:14 -06:00
print "<tr class='tun_client_list2 tun_client_row'>";
print "<td class='tun_client_center_item' rowspan='2'>";
2015-04-01 09:42:53 -06:00
# Required to be first, so, if the checkbox is cleared, a value will still POST
2015-04-08 18:04:43 -06:00
print "<input type='hidden' name='client${val}_enabled' value='0'>" unless($val eq "_add");
print "<input type='checkbox' name='client${val}_enabled' value='1'";
2015-04-01 09:42:53 -06:00
print " onChange='form.submit()'" unless $val eq "_add";
2015-04-08 18:04:43 -06:00
print " checked='checked'" if $enabled;
2015-04-01 09:42:53 -06:00
print " title='enable this client'></td>";
2016-05-15 16:40:29 -06:00
print "<td><input type=text size=40 name=client${val}_name value='$name'";
2015-04-01 09:42:53 -06:00
print " onChange='form.submit()'" unless $val eq "_add";
# print " disabled" unless $val eq "_add";
print " title='client name'></td>";
2020-03-13 15:40:14 -06:00
print "<td><input type=text size=25 name=client${val}_passwd value='$passwd' ";
2015-04-01 09:42:53 -06:00
print " onChange='form.submit()'" unless $val eq "_add";
print " title='client password'";
#print " disabled" unless $val eq "_add";
print "></td>";
# handle rollover of netw[3]
2015-05-07 21:55:15 -06:00
if($netw[3]+($cnum * 4) > 252) {
$netw[2]++;
2015-04-01 09:42:53 -06:00
$netw[3] = 0;
$net=0;
$cnum=0;
} else {
$net=$cnum;
}
2015-05-07 21:55:15 -06:00
if($val eq "_add") { $lastnet=$netw[3]+(($net) * 4); }
else { $lastnet=$netw[3]+($net * 4); }
$fullnet=sprintf("%d.%d.%d.%d",$netw[0],$netw[1],$netw[2],$lastnet);
2020-03-13 15:40:14 -06:00
print "<td rowspan='2' class='tun_client_center_item'> $fullnet";
2015-04-20 22:53:57 -06:00
print "<input type=hidden name=client${val}_netip value='$fullnet'/></td>";
2020-03-13 15:40:14 -06:00
print "<td rowspan='2' class='tun_client_center_item' align=center> ";
2015-06-14 19:50:54 -06:00
if (&is_tunnel_active($fullnet,@active_tun) && ($val ne "_add")) {
2015-06-14 22:30:06 -06:00
print "<img class='tun_client_active_img' src='/connected.png' title='Connected' />";
2015-06-14 19:50:54 -06:00
} else {
2015-06-14 22:30:06 -06:00
print "<img class='tun_client_inactive_img' src='/disconnected.png' title='Not connected' />";
2015-06-14 19:50:54 -06:00
}
2015-04-01 09:42:53 -06:00
print "</td>";
2020-03-13 15:40:14 -06:00
print "<td rowspan='2' class='tun_client_center_item'><input type=submit name=client_add value=Add title='Add this client'>" if($val eq "_add");
2015-04-01 09:42:53 -06:00
print "</td>";
2020-03-13 15:40:14 -06:00
print "<td rowspan='2' class='tun_client_center_item tun_client_mailto'><a href='mailto:?subject=AREDN%20Tunnel%20Connection&body=Your%20connection%20details:%0D%0AName:%20$name%0D%0APassword:%20$passwd%0D%0ANetwork:%20$fullnet%0D%0AServer%20address:%20$dns'><img class='tun_client_mailto_img' src='/email.png' title='Email details' /></a></td>" unless($val eq "_add");
#contact info for the tunnel
print "</tr>";
print "<tr class='tun_client_list1 tun_client_row tun_loading_css_comment'><td colspan='2' align='right'>Contact Info/Comment (Optional): <input type=text maxlength='50' size=40 name=client${val}_contact value='$contact'";
print " onChange='form.submit()'" unless ($val eq "_add" || $val eq "");
print " title='client contact info'></td>";
2015-04-01 09:42:53 -06:00
print "</tr>\n";
# display any errors
while(@cli_err and $cli_err[0] =~ /^$val /)
{
$err = shift @cli_err;
$err =~ s/^\S+ //;
2015-04-08 18:04:43 -06:00
print "<tr class=tun_client_error_row><th colspan=4>$err</th></tr>\n";
2015-04-01 09:42:53 -06:00
}
#push @hidden, "<input type='hidden' name='client${val}_enable' value='0'>" unless($val eq "_add");
print "<tr><td colspan=4 height=4></td></tr>\n";
$cnum++;
}
print "</table>\n";
}
#################################
2015-04-08 18:04:43 -06:00
# load client info from UCI
2015-04-01 09:42:53 -06:00
#################################
sub get_client_info()
{
2015-05-06 21:01:28 -06:00
my @clients=&uci_get_names_by_sectiontype("vtun","client");
my $c=0;
foreach (@clients)
2015-04-01 09:42:53 -06:00
{
2015-05-06 21:01:28 -06:00
my $myclient={};
$myclient=&uci_get_named_section("vtun",$_);
2020-03-13 15:40:14 -06:00
foreach $var (qw(enabled name passwd netip contact))
2015-04-01 09:42:53 -06:00
{
2015-05-06 21:01:28 -06:00
$parms{"client${c}_$var"} = $myclient->{$var};
2015-04-08 18:04:43 -06:00
$parms{"client${c}_$var"} = "0" if($parms{"client${c}_$var"} eq "");
2020-03-13 15:40:14 -06:00
$myclient->{$var} = "";
2015-04-01 09:42:53 -06:00
}
2015-05-06 21:01:28 -06:00
$c++;
2015-04-01 09:42:53 -06:00
}
2015-04-08 18:04:43 -06:00
$parms{client_num} = scalar(@clients);
2015-04-01 09:42:53 -06:00
}
sub DEBUGEXIT()
{
my ($text) = @_;
http_header();
html_header("$node setup", 1);
print "DEBUG-";
print $text;
print "</body>";
exit;
2015-05-10 15:38:03 -06:00
}