reworked ucifunc.pm

added server DNS name field
reworked to use section names in ici
This commit is contained in:
Darryl Quinn 2015-05-06 22:01:28 -05:00
parent cc1f683121
commit 5a246f7441
4 changed files with 205 additions and 69 deletions

View File

@ -33,6 +33,14 @@
=cut
sub get_server_dns()
{
my @list;
my $uciresult;
my ($rc,$dns)=&uci_get_indexed_option("vtun","network","0","dns");
return $dns;
}
#################################
# get base network from config
#################################
@ -97,19 +105,22 @@ sub is_tunnel_active()
# Add OLSRD interfaces
##########################
sub add_olsrd_interfaces() {
my ($tunstart,$tuncount) = @_;
&uci_add_named_section("olsrd","tunnelserver","Interface");
my ($sname,$tunstart,$tuncount) = @_;
my $tuns;
&uci_set_named_option("olsrd","tunnelserver","Ip4Broadcast","255.255.255.255");
&uci_add_named_section("olsrd",$sname,"Interface");
&uci_set_named_option("olsrd",$sname,"Ip4Broadcast","255.255.255.255");
# delete all interfaces first
&uci_delete_named_option("olsrd","tunnelserver","interfaces");
for (my $i=$tunstart, $i<$tuncount, $i++) {
&uci_add_list_named_option("olsrd","tunnelserver","interfaces","tun${i}");
&uci_delete_named_option("olsrd",$sname,"interfaces");
for my $i (0..$tuncount-1) {
$tuns=$tuns . " " if $i;
$tuns=$tuns . "tun" . $tunstart;
$tunstart++;
}
&uci_add_list_named_option("olsrd",$sname,"interfaces","$tuns");
&uci_commit("olsrd");
}

View File

@ -33,7 +33,8 @@
=cut
### UCI Helpers START ###
### UCI Helpers --GET-- --GET-- --GET-- --GET-- --GET-- --GET-- --GET-- --GET-- --GET-- --GET-- --GET-- ###
sub uci_get_sectiontype_count()
{
my ($config, $stype)=@_;
@ -64,8 +65,90 @@ sub uci_get_indexed_sectiontype()
return ($rc, @res);
}
# Returns an array of section names
sub uci_get_names_by_sectiontype()
{
my ($config,$stype)=@_;
my @names=();
my $cmd=sprintf('uci show %s|egrep vtun\..*=%s',$config,$stype);
my @lines=`$cmd`;
if (scalar @lines) {
foreach $l (0..@lines-1) {
@parts=();
chomp(@lines[$l]);
@parts = @lines[$l] =~ /^$config\.(.*)\=$stype/g;1;
if (scalar(@parts) eq 1) {
push(@names,@parts[0]);
}
}
}
return @names;
}
# Returns all lines of config for a named section
sub uci_get_named_section()
{
my ($config,$sname)=@_;
my $cmd=sprintf('uci show %s.%s',$config,$sname);
my @lines=`$cmd`;
my %section;
if (scalar @lines) {
foreach (@lines)
{
$l=$_;
chomp($l);
# @parts=();
@parts = $l =~ /^$config\.$sname\.(.*)\=(.*)/g;1;
if (scalar(@parts) eq 2) {
$section->{@parts[0]} = @parts[1];
}
}
}
return $section;
}
## is this function still needed
sub uci_get_all_named_by_sectiontype()
{
my ($config,$stype)=@_;
my @sections=();
my $cmd=sprintf('uci show %s|grep \=%s',$config,$config,$stype);
my @lines=`$cmd`;
## DLQ - need to get the names by stype
## then get each one to iterate over
if (scalar @lines) {
my $lastindex=0;
my $sect={};
my @parts=();
foreach $l (0..@lines-1) {
@parts=();
chomp(@lines[$l]);
@parts = @lines[$l] =~ /^$config\.(.*)\.\=$stype/g;1;
if (scalar(@parts) eq 1) {
if (@parts[0] ne $lastindex) {
push @sections, $sect;
$sect={};
$lastindex=@parts[0];
}
$sect->{@parts[1]} = @parts[2];
next;
}
}
push (@sections, $sect);
}
return (@sections);
}
# RETURNS an array of hashes
sub uci_get_all_by_sectiontype()
sub uci_get_all_indexed_by_sectiontype()
{
my ($config,$stype)=@_;
my @sections=();
@ -96,17 +179,40 @@ sub uci_get_all_by_sectiontype()
return (@sections);
}
### UCI Helpers --ADD-- ###
sub uci_add_sectiontype()
{
my ($config,$stype)=@_;
system `touch /etc/config/$config` if (! -f "/etc/config/$config");
my $cmd=sprintf('uci add %s %s',$config,$stype);
my $res=`$cmd`;
my $rc=$?;
return ($rc);
return $rc;
}
sub uci_add_list_named_option()
{
my ($config,$sname,$option,$val)=@_;
my $cmd=sprintf('uci add_list %s.%s.%s=\'%s\'',$config,$sname,$option,$val);
my $res=`$cmd`;
my $rc=$?;
return $rc;
}
sub uci_add_named_section()
{
my ($config,$sname,$stype)=@_;
my $cmd=sprintf('uci set %s.%s=%s',$config,$sname,$stype);
#uci set olsrd.tunnelserver=Interface
my $res=`$cmd`;
my $rc=$?;
return $rc;
}
### UCI Helpers --DELETE-- ###
sub uci_delete_option()
{
my ($config,$stype,$index,$option)=@_;
@ -117,38 +223,36 @@ sub uci_delete_option()
return ($rc,$res);
}
sub uci_add_list_named_option()
{
my ($config,$sname,$option,$val)=@_;
my $cmd=sprintf('uci add_list %s.%s.%s=\'%s\'',$config,$sname,$option,$val);
my $rc=$?;
return ($rc);
}
sub uci_delete_named_option()
{
my ($config,$sname,$option)=@_;
my $cmd=sprintf('uci delete %s.%s.%s',$config,$sname,$option);
my $res=`$cmd`;
my $rc=$?;
return ($rc);
return $rc;
}
sub uci_add_named_section()
sub uci_delete_indexed_type()
{
my ($config,$sname,$stype)=@_;
my $cmd=sprintf('uci set %s.%s=%s',$config,$sname,$stype);
#uci set olsrd.tunnelserver=Interface
my ($config,$stype,$index)=@_;
my $cmd=sprintf('uci delete %s.@%s[%s]',$config,$stype,$index);
my $res=`$cmd`;
my $rc=$?;
return ($rc);
chomp($res);
return ($rc,$res);
}
### UCI Helpers --SET-- ###
sub uci_set_named_option()
{
my ($config,$sname,$option,$val)=@_;
my $cmd=sprintf('uci set %s.%s.%s=%s',$config,$sname,$option,$val);
#uci set olsrd.tunnelserver.Ip4Broadcast=255.255.255.255
my $res=`$cmd`;
my $rc=$?;
return ($rc);
return $rc;
}
sub uci_set_indexed_option()
@ -167,15 +271,8 @@ sub uci_set_indexed_option()
return $rc;
}
sub uci_delete_indexed_type()
{
my ($config,$stype,$index)=@_;
my $cmd=sprintf('uci delete %s.@%s[%s]',$config,$stype,$index);
my $res=`$cmd`;
my $rc=$?;
chomp($res);
return ($rc,$res);
}
### UCI Helpers --OTHER-- ###
sub uci_commit()
{

View File

@ -44,6 +44,7 @@ $config = nvram_get("config");
$node = nvram_get("node");
$node = "NOCALL" if $node eq "";
$unode = uc $node; # UPPER CASE NODENAME
$tun_server_start_num=50;
read_postdata();
@ -72,6 +73,7 @@ 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");
}
@ -80,6 +82,7 @@ if($parms{button_reset})
#################
@netw = ();
@netw = get_server_network_address();
$dns = get_server_dns();
#################
# If RESET or FIRST TIME, load clients/servers from file into parms
@ -94,6 +97,8 @@ if($parms{button_reset} or not $parms{reload})
$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))
@ -174,10 +179,11 @@ foreach $val (@list)
$parms{client_num} = $client_num;
#################
# SAVE the server network numbers into the UCI
# 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();
#################
@ -300,7 +306,7 @@ exit;
##################
######################################################
# List the clients allowed to connect to this server # - CHANGE TO UCI
# List the clients allowed to connect to this server
######################################################
sub print_vpn_clients()
{
@ -311,7 +317,11 @@ sub print_vpn_clients()
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 " (must be between 0 and 254)";
print "<br /><hr>Tunnel Server DNS Name: ";
print "<input type='text' name='dns' size='30' value='$dns' onChange='form.submit()' ></td></tr>";
print "</table>";
print "<hr />";
print "<table class=tun_client_table cellpadding=0 cellspacing=0>";
@ -375,7 +385,7 @@ sub print_vpn_clients()
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$passwd%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");
print "<td class='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'/></a></td>" unless($val eq "_add");
print "</tr>\n";
# display any errors
@ -399,15 +409,18 @@ sub print_vpn_clients()
#################################
sub get_client_info()
{
my @clients=&uci_get_all_by_sectiontype("vtun","client");
foreach $c (0..@clients-1)
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"} = @clients[$c]->{$var};
$parms{"client${c}_$var"} = $myclient->{$var};
$parms{"client${c}_$var"} = "0" if($parms{"client${c}_$var"} eq "");
}
$c++;
}
$parms{client_num} = scalar(@clients);
@ -422,40 +435,41 @@ sub save_clients()
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_indexed_option("vtun","client",$i,"netip",$net);
push(@cli_err,"Problem saving UCI vtun client net IP (#$i)") if $rc;
$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_indexed_option("vtun","client",$i,"enabled",$parms{"client${i}_enabled"});
push(@cli_err,"Problem saving UCI vtun client (#$i)") 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_indexed_option("vtun","client",$i,"name",$parms{"client${i}_name"});
push(@cli_err,"Problem saving UCI vtun client (#$i)") 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_indexed_option("vtun","client",$i,"passwd",$parms{"client${i}_passwd"});
push(@cli_err,"Problem saving UCI vtun client (#$i)") 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_indexed_option("vtun","client",$i,"clientip",$clientip);
push(@cli_err,"Problem saving UCI vtun client client IP (#$i)") if $rc;
$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_indexed_option("vtun","client",$i,"serverip",$serverip);
push(@cli_err,"Problem saving UCI vtun client server IP (#$i)") 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_indexed_option("vtun","client",$i,"node",$vtun_node_name);
push(@cli_err,"Problem saving UCI vtun client name (#$i)") 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"};
}
# add enabled interfaces to OLSRD
&add_olsrd_interfaces(50,$enabled_count) if($enabled_count);
&add_olsrd_interfaces("tunnelserver",$tun_server_start_num,$enabled_count) if($enabled_count > 0);
}
#################################
@ -465,6 +479,8 @@ 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()

View File

@ -44,6 +44,7 @@ $config = nvram_get("config");
$node = nvram_get("node");
$node = "NOCALL" if $node eq "";
$unode = uc $node; # UPPER CASE NODENAME
$tun_client_start_num=60;
read_postdata();
@ -370,14 +371,18 @@ sub print_vpn_connections()
#################################
sub get_connection_info()
{
my @connections=&uci_get_all_by_sectiontype("vtun","server");
foreach $c (0..@connections-1)
my @connections=&uci_get_names_by_sectiontype("vtun","server");
my $c=0;
foreach (@connections)
{
my $myconn={};
$myconn=&uci_get_named_section("vtun",$_);
foreach $var (qw(enabled host passwd netip))
{
$parms{"conn${c}_$var"} = @connections[$c]->{$var};
$parms{"conn${c}_$var"} = $myconn->{$var};
$parms{"conn${c}_$var"} = "0" if($parms{"conn${c}_$var"} eq "");
}
$c++;
}
$parms{conn_num} = scalar(@connections);
@ -388,10 +393,13 @@ sub get_connection_info()
#################################
sub save_connections()
{
my $enabled_count=0;
for ($i=0; $i < $parms{"conn_num"}; $i++) {
my $net = $parms{"conn${i}_netip"};
$rc=&uci_add_named_section("vtun","server_$i","server");
# generate the clientip and serverip
my ($clientip, $serverip) = &generate_ips($net);
@ -399,21 +407,25 @@ sub save_connections()
$net=~ s/\./\-/g;
my $vtun_node_name="$node-$net";
$rc=&uci_set_indexed_option("vtun","server",$i,"clientip",$clientip);
$rc=&uci_set_named_option("vtun","server_$i","clientip",$clientip);
push(@cli_err,"Problem saving UCI vtun connection client IP (#$i)") if $rc;
$rc=&uci_set_indexed_option("vtun","server",$i,"serverip",$serverip);
$rc=&uci_set_named_option("vtun","server_$i","serverip",$serverip);
push(@cli_err,"Problem saving UCI vtun connection server IP (#$i)") if $rc;
$rc=&uci_set_indexed_option("vtun","server",$i,"node",$vtun_node_name);
$rc=&uci_set_named_option("vtun","server_$i","node",$vtun_node_name);
push(@cli_err,"Problem saving UCI vtun connection name (#$i)") if $rc;
foreach $var (qw(enabled host passwd netip))
{
$rc=&uci_set_indexed_option("vtun","server",$i,$var,$parms{"conn${i}_$var"});
$rc=&uci_set_named_option("vtun","server_$i",$var,$parms{"conn${i}_$var"});
push(@cli_err,"Problem saving UCI vtun connection (#$i)") if $rc;
}
$enabled_count++ if $parms{"conn${i}_enabled"};
}
# add enabled interfaces to OLSRD
&add_olsrd_interfaces("tunnelclient",$tun_client_start_num,$enabled_count) if($enabled_count > 0);
}
sub DEBUGEXIT()