aredn/files/www/cgi-bin/vpn

416 lines
12 KiB
Plaintext
Raw Normal View History

2015-04-01 09:42:53 -06:00
#!/usr/bin/perl
$debug = 1;
2015-04-01 09:42:53 -06:00
BEGIN {push @INC, '/www/cgi-bin'};
use perlfunc;
use ucifunc;
use tunfunc;
$VPNVER="1.0";
$config = nvram_get("config");
$node = nvram_get("node");
$node = "NOCALL" if $node eq "";
$unode = uc $node; # UPPER CASE NODENAME
#$vpncfile = "/etc/vpnclients";
#$tmpdir = "/tmp/web/vpn";
#$tmpvtundsconf = "${tmpdir}/vtundsrv.conf.tmp";
#$tmpconnfile = "${tmpdir}/vpnclients";
#$tsfile = "/etc/tunnel.server";
#$tmptsfile = "${tmpdir}/tunnel.server.tmp";
2015-04-01 09:42:53 -06:00
read_postdata();
#################
# page checks
#################
if($parms{button_reboot})
{
system "/sbin/reboot";
}
if($parms{button_install})
{
install_vtun();
2015-04-01 09:42:53 -06:00
}
reboot_required() if($config eq "" or -e "/tmp/reboot-required");
vpn_setup_required() unless(-e "/usr/sbin/vtund" );
get_active_tun();
2015-04-01 09:42:53 -06:00
#################
# If RESET, revert the UCI file
2015-04-01 09:42:53 -06:00
#################
if($parms{button_reset})
{
($rc,$res)=&uci_revert("vtun");
($rc,$res)=&uci_delete_option("vtun","network",0,"start");
$rc=&uci_commit("vtun");
2015-04-01 09:42:53 -06:00
}
#################
# get vtun network address
#################
@netw = ();
@netw = get_server_network_address();
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})
{
# revert to previous state on initial load
($rc,$res)=&uci_revert("vtun");
# load clients from UCI
2015-04-01 09:42:53 -06:00
&get_client_info();
$parms{server_net1}=@netw[2];
$parms{server_net2}=@netw[3];
# initialize the "add" entries to clear them
foreach $var (qw(client_add_enabled client_add_name client_add_passwd))
2015-04-01 09:42:53 -06:00
{
$parms{$var} = "";
$parms{$var} = "0" if($var eq 'client_add_enabled');
2015-04-01 09:42:53 -06:00
}
}
#################
# load clients from FORM and validate
#################
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)
{
foreach $var (qw(enabled name passwd))
2015-04-01 09:42:53 -06:00
{
$varname = "client${val}_$var";
$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")
{
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
next unless ($enabled or $name or $passwd) 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
if($val eq "_add" and $parm{button_save})
{
push @cli_err, "$val this client must be added or cleared out before saving changes";
next;
}
# password MUST be alphanumeric (no special chars)
push @cli_err, "The password cannot contain non-alphanumeric characters (#$client_num)" if ($passwd =~ m/[^a-zA-Z0-9]/);
2015-04-01 09:42:53 -06:00
push @cli_err, "A client name is required" if($name eq "");
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 /;
$parms{"client${client_num}_enabled"} = $enabled;
2015-04-01 09:42:53 -06:00
$parms{"client${client_num}_name"} = uc $name;
$parms{"client${client_num}_passwd"} = $passwd;
2015-04-01 09:42:53 -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")
{
foreach $var (qw(net enabled name passwd))
2015-04-01 09:42:53 -06:00
{
$parms{"client_add_${var}"} = "";
}
}
}
$parms{client_num} = $client_num;
2015-04-01 09:42:53 -06:00
#################
# SAVE the clients
2015-04-01 09:42:53 -06:00
#################
$rc=save_clients();
2015-04-01 09:42:53 -06:00
#################
# SAVE the server network numbers into the UCI
2015-04-01 09:42:53 -06:00
#################
$netw[2]=$parms{server_net1};
$netw[3]=$parms{server_net2};
$rc=save_network();
2015-04-01 09:42:53 -06:00
#################
# save configuration (commit)
2015-04-01 09:42:53 -06:00
#################
if($parms{button_save} and not (@cli_err or @serv_err))
{
if (&uci_commit("vtun"))
{
push(@errors,"Problem committing UCI vtun");
system("cp -f /etc/config/vtun /etc/config.mesh");
}
2015-04-01 09:42:53 -06:00
unless($debug == 3)
{
push(@errors,"Problem restaring vtundsrv") if system "/etc/init.d/vtundsrv restart > /dev/null 2>&1";
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";
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 "&nbsp;&nbsp;&nbsp;\n";
print "<input type=submit name=button_save value='Save Changes' title='Save and use these settings now (takes about 20 seconds)'>&nbsp;\n";
print "<input type=submit name=button_reset value='Reset Values' title='Revert to the last saved settings'>&nbsp;\n";
print "<input type=submit name=button_refresh value='Refresh' title='Refresh this page'>&nbsp;\n";
print "<tr><td>&nbsp;</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";
}
elsif(@errors)
{
print "<tr><td align=center><b>Configuration saved, however:<br>";
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>&nbsp;</td></tr>\n";
}
#################
# everything else
#################
if($config eq "mesh")
{
2015-04-01 09:42:53 -06:00
print "<tr><td align=center valign=top>\n";
&print_vpn_clients();
print "</td></tr>\n";
2015-04-01 09:42:53 -06:00
print "<tr><td><hr></td></tr>\n";
}
print "</table>\n";
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
#################
print "</body></html>\n";
exit;
##################
# page subsections
##################
######################################################
# List the clients allowed to connect to this server # - CHANGE TO UCI
2015-04-01 09:42:53 -06:00
######################################################
sub print_vpn_clients()
{
print "<table cellpadding=0 cellspacing=0>";
2015-04-01 09:42:53 -06:00
print "<br /><tr class=tun_network_row><td colspan=6 align=center valign=top>Tunnel Server Network: ";
2015-04-01 09:42:53 -06:00
printf("%d.%d.",@netw[0],@netw[1]);
print "<input type='text' name='server_net1' size='3' maxlen='3' value='@netw[2]' onChange='form.submit()' >";
print ".";
print "<input type='text' name='server_net2' size='3' maxlen='3' value='@netw[3]' onChange='form.submit()'>";
print " (must be between 0 and 254)</td></tr>";
print "</table>";
print "<hr />";
print "<table class=tun_client_table cellpadding=0 cellspacing=0>";
print "<tr><th colspan=6 align=center valign=top>&nbsp;</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";
print "<tr><th>Enabled?</th><th>Client</th><th>Pwd</th><th>Net</th><th>Active&nbsp;</td><th>Action</th></tr>\n";
for($i = 0, @list = (); $i < $parms{client_num}; ++$i) { push @list, $i };
2015-04-01 09:42:53 -06:00
push @list, "_add" unless($parms{client_num} > 9);
$cnum=0;
foreach $val (@list)
{
foreach $var (qw(enabled name passwd))
2015-04-01 09:42:53 -06:00
{
eval sprintf("\$%s = \$parms{client%s_%s}", $var, $val, $var);
}
print "<tr class=tun_client_add_row><td height=10></td></tr>\n" if $val eq "_add" and scalar(@list) > 1;
print "<tr class=tun_client_row>";
2015-04-01 09:42:53 -06:00
print "<td>";
# Required to be first, so, if the checkbox is cleared, a value will still POST
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";
print " checked='checked'" if $enabled;
2015-04-01 09:42:53 -06:00
print " title='enable this client'></td>";
print "<td><input type=text size=25 name=client${val}_name value='$name'";
print " onChange='form.submit()'" unless $val eq "_add";
# print " disabled" unless $val eq "_add";
print " title='client name'></td>";
print "<td>&nbsp;&nbsp;<input type=text size=20 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]
if(@netw[3]+($cnum * 4) > 252) {
@netw[2]++;
$netw[3] = 0;
$net=0;
$cnum=0;
} else {
$net=$cnum;
}
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);
print "<td>&nbsp;$fullnet</td>";
print "<td align=center>&nbsp;";
print "<img class='tun_client_active_img' src='/plug-connect.png'/>" if (&is_tunnel_active($name) && ($val ne "_add"));
print "<img class='tun_client_inactive_img' src='/plug-disconnect.png'/>" if ($val ne "_add");
2015-04-01 09:42:53 -06:00
print "</td>";
print "<td><input type=submit name=client_add value=Add title='Add this client'>" if($val eq "_add");
print "</td>";
print "<td class='tun_client_mailto'><a href='mailto:?subject=AREDN%20Tunnel%20Connection&body=Your%20connection%20details:%0D%0AName:%20$name%0D%0APassword:%20$pass%0D%0ANetwork:%20$fullnet%0D%0AServer%20address:%20<your%20server%20dns%20name>'><img class='tun_client_mailto_img' src='/email.png'/></a></td>" unless($val eq "_add");
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+ //;
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";
}
#################################
# load client info from UCI
2015-04-01 09:42:53 -06:00
#################################
sub get_client_info()
{
my @clients=&uci_get_all_by_sectiontype("vtun","client");
foreach $c (0..@clients-1)
2015-04-01 09:42:53 -06:00
{
foreach $var (qw(enabled name passwd))
2015-04-01 09:42:53 -06:00
{
$parms{"client${c}_$var"} = @clients[$c]->{$var};
$parms{"client${c}_$var"} = "0" if($parms{"client${c}_$var"} eq "");
2015-04-01 09:42:53 -06:00
}
}
$parms{client_num} = scalar(@clients);
2015-04-01 09:42:53 -06:00
}
#################################
# save clients from form to UCI
#################################
sub save_clients()
{
for ($i=0; $i < $parms{"client_num"}; $i++) {
foreach $var (qw(enabled name passwd))
{
$rc=&uci_set_indexed_option("vtun","client",$i,$var,$parms{"client${i}_$var"});
push(@errors,"Problem saving UCI vtun client (#$i)") if $rc;
}
}
}
#################################
# save network info to UCI
#################################
sub save_network()
{
my $net=sprintf("%d.%d.%d.%d",172,31,$parms{server_net1},$parms{server_net2});
#push @cli_err, "The server net values must be between 0-254!";
push @errors, "Problem saving the server network values!" if (&uci_set_indexed_option("vtun","network",0,"start",$net));
}
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;
}