#!/usr/bin/perl =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 . 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. You must not misrepresent the origin of the material conained within. 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 $debug = 0; 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 ""; read_postdata(); ################# # page checks ################# if($parms{button_reboot}) { system "/sbin/reboot"; } if($parms{button_install}) { install_vtun(); } reboot_required() if($config eq "" or -e "/tmp/reboot-required"); &vpn_setup_required("vpn") unless(-e "/usr/sbin/vtund" ); @active_tun=&get_active_tun(); ################# # If RESET, revert the UCI file ################# if($parms{button_reset}) { ($rc,$res)=&uci_revert("vtun"); ($rc,$res)=&uci_delete_option("vtun","network",0,"start"); ($rc,$res)=&uci_delete_option("vtun","network",0,"dns"); $rc=&uci_commit("vtun"); } ################# # get vtun network address ################# @netw = (); @netw = get_server_network_address(); $dns = get_server_dns(); ################# # 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 &get_client_info(); $parms{server_net1}=$netw[2]; $parms{server_net2}=$netw[3]; $parms{dns}=$dns; # initialize the "add" entries to clear them foreach $var (qw(client_add_enabled client_add_name client_add_passwd)) { $parms{$var} = ""; $parms{$var} = "0" if($var eq 'client_add_enabled'); } } ################# # load clients from FORM and validate ################# for($i =0 , @list = (); $i < $parms{client_num}; $i++) { push @list, $i } push @list, "_add"; $client_num = 0; foreach $val (@list) { foreach $var (qw(enabled name passwd netip)) { $varname = "client${val}_$var"; $parms{$varname} = "0" if($val eq "enabled" and $parms{$varname} eq ""); $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")) { $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}); } # 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@]/); push @cli_err, "A client name is required" if($name eq ""); push @cli_err, "A client password is required" if($passwd eq ""); next if $val eq "_add" and @cli_err and $cli_err[-1] =~ /^$val /; $parms{"client${client_num}_enabled"} = $enabled; $parms{"client${client_num}_name"} = uc $name; $parms{"client${client_num}_passwd"} = $passwd; $parms{"client${client_num}_netip"} = $netip; # Commit the data for this client $client_num++; # Clear out the ADD values if($val eq "_add") { foreach $var (qw(net enabled name passwd netip)) { $parms{"client_add_${var}"} = ""; } } } $parms{client_num} = $client_num; ################# # SAVE the server network numbers and dns into the UCI ################# $netw[2]=$parms{server_net1}; $netw[3]=$parms{server_net2}; $dns=$parms{dns}; $rc=save_network(); ################# # SAVE the clients ################# $rc=save_clients(); ################# # save configuration (commit) ################# if($parms{button_save} and not @cli_err) { if (&uci_commit("vtun")) { push(@errors,"Problem committing UCI vtun"); } &uci_clone("vtun"); unless($debug == 3) { # Regenerate olsrd files and restart olsrd push(@errors,"Problem restarting olsrd") if system "/etc/init.d/olsrd restart > /dev/null 2>&1"; push(@errors,"Problem restaring vtundsrv") if system "/etc/init.d/vtundsrv restart > /dev/null 2>&1"; } } ###################################################################################### # generate the page ###################################################################################### http_header() unless $debug == 2; html_header("$node setup", 1); print "
\n"; alert_banner(); print "
\n" unless $debug == 2; print "\n" if $debug == 2; print "\n"; ################# # Navigation bar ################# print "\n"; ################# # control buttons ################# print "\n"; push @hidden, ""; ################# # messages ################# if(@cli_err) { print "\n"; } if($parms{button_save}) { if(@cli_err) { print "\n"; #} #elsif(@errors) #{ #print "\n"; } else { print "\n"; } print "\n"; } ################# # everything else ################# if($config eq "mesh") { print "\n"; print "\n"; } print "
\n"; navbar("vpn"); print "
"; print "Help"; print "   \n"; print " \n"; print " \n"; print " \n"; print "
 
ERROR:
"; foreach(@cli_err) { print "$_
" } print "
Configuration NOT saved!
Configuration saved, however:
"; foreach(@errors) { print "$_
" } print "
Configuration saved and is now active.
 
\n"; &print_vpn_clients(); print "

\n"; print "

Tunnel v${VPNVER}

"; push @hidden, ""; ################# # add hidden form fields ################# foreach(@hidden) { print "$_\n" } ################# # close the form ################# print "
\n"; show_debug_info(); ################# # close the html ################# page_footer(); print "\n"; exit; ################## # page subsections ################## ###################################################### # List the clients allowed to connect to this server ###################################################### sub print_vpn_clients() { print ""; print "
"; print "
Tunnel Server Network: "; printf("%d.%d.",$netw[0],$netw[1]); print ""; print "."; print ""; print " (must be between 0 and 254)"; print "

Tunnel Server DNS Name: "; print "
"; print "
"; print ""; print "\n"; print ""; print "\n"; print "\n"; for($i = 0, @list = (); $i < $parms{client_num}; ++$i) { push @list, $i }; push @list, "_add" unless($parms{client_num} > 9); $cnum=0; foreach $val (@list) { foreach $var (qw(enabled name passwd)) { eval sprintf("\$%s = \$parms{client%s_%s}", $var, $val, $var); } print "\n" if $val eq "_add" and scalar(@list) > 1; print ""; print ""; print ""; print ""; # 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 ""; print ""; print ""; print "" unless($val eq "_add"); print "\n"; # display any errors while(@cli_err and $cli_err[0] =~ /^$val /) { $err = shift @cli_err; $err =~ s/^\S+ //; print "\n"; } #push @hidden, "" unless($val eq "_add"); print "\n"; $cnum++; } print "
 
Allow the following clients to connect to this server:
Enabled?ClientPwdNetActive Action
"; # Required to be first, so, if the checkbox is cleared, a value will still POST print "" unless($val eq "_add"); print "   $fullnet"; print " "; print "" if (&is_tunnel_active($name,@active_tun) && ($val ne "_add")); print "" if ($val ne "_add"); print "" if($val eq "_add"); print "
$err
\n"; } ################################# # load client info from UCI ################################# sub get_client_info() { my @clients=&uci_get_names_by_sectiontype("vtun","client"); my $c=0; foreach (@clients) { my $myclient={}; $myclient=&uci_get_named_section("vtun",$_); foreach $var (qw(enabled name passwd netip)) { $parms{"client${c}_$var"} = $myclient->{$var}; $parms{"client${c}_$var"} = "0" if($parms{"client${c}_$var"} eq ""); } $c++; } $parms{client_num} = scalar(@clients); } ################################# # 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"}); push(@cli_err,"Problem saving UCI vtun client (#$i): $rc") if $rc; $rc=&uci_set_named_option("vtun","client_$i","name",$parms{"client${i}_name"}); push(@cli_err,"Problem saving UCI vtun client (#$i): $rc") if $rc; $rc=&uci_set_named_option("vtun","client_$i","passwd",$parms{"client${i}_passwd"}); push(@cli_err,"Problem saving UCI vtun client (#$i): $rc") if $rc; # 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"}; } } ################################# # 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, "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)); } sub DEBUGEXIT() { my ($text) = @_; http_header(); html_header("$node setup", 1); print "DEBUG-"; print $text; print ""; exit; }