#!/usr/bin/perl =for commnet Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks Copyright (C) 2015 Conrad Lara See Contributors file for additional contributors Copyright (c) 2013 David Rivenburg et al. BroadBand-HamNet 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 channelmaps; # # load the config parms # # test for web connectivity (for maps) $pingOk=is_online(); @output = (); @errors = (); read_postdata(); ($config = $parms{config}) or ($config = nvram_get("config") and -d "/etc/config.$config") or ($config = "mesh"); my $tz_db_strings = tz_names_hash(); my $tz_db_names = tz_names_array(); if($parms{button_uploaddata}) { my $si=`curl 'http://localnode:8080/cgi-bin/sysinfo.json?hosts=1' 2>/dev/null`; # strip closing }\n from si chomp($si); chop($si); # get olsrd topo information my $topo=`curl 'http://localnode:9090/topology' 2>/dev/null`; chomp($topo); # add topo subdoc and close root doc my $newsi= sprintf "%s,\"olsr\": %s}",$si, $topo; # PUT it to the server my $upcurl=`curl -H 'Accept: application/json' -X PUT -d '$newsi' http://data.aredn.org/sysinfo`; if($? == 0) { push @output, "AREDN online map updated"; } else { push @errors, "ERROR: Cannot update online map. Please ensure this node has access to the internet."; } } # convert the %parms into scalars for convenience if($parms{button_default}) { load_cfg("/etc/config.$config/_setup.default"); foreach(keys %cfg) { eval (sprintf "\$$_ = \"%s\"", quotemeta $cfg{$_}); } } else { foreach(keys %parms) { next unless /^\w+$/; $parms{$_} =~ s/^\s+//; $parms{$_} =~ s/\s+$//; eval (sprintf "\$$_ = \"%s\"", quotemeta $parms{$_}); } if($button_reset or $config ne $oldconfig) { load_cfg("/etc/config.$config/_setup"); foreach(keys %cfg) { eval (sprintf "\$$_ = \"%s\"", quotemeta $cfg{$_}); } } } if($parms{button_reset} or $parms{button_default} or (not $nodetac and not keys %parms)) { $nodetac = nvram_get("node"); $tactical = nvram_get("tactical"); $nodetac .= " / $tactical" if $tactical; } else { $nodetac = $parms{nodetac}; } # make sure unchecked checkboxes are accounted for foreach(qw(lan_dhcp olsrd_bridge olsrd_gw wifi_hidden)) { $parms{$_} = 0 unless $parms{$_}; } # lan is always static $lan_proto = "static"; # enforce direct mode settings # (formerly known as dmz mode) $dmz_mode = 0 unless $config eq "mesh"; $dmz_mode = 2 if $dmz_mode != 0 and $dmz_mode < 2; $dmz_mode = 4 if $dmz_mode > 4; if($dmz_mode) { $ipshift = (ip2decimal($wifi_ip) << $dmz_mode) & 0xffffff; $dmz_lan_ip = add_ip_address("1" . decimal2ip($ipshift), 1); $dmz_lan_mask = decimal2ip(0xffffffff << $dmz_mode); ($octet) = $dmz_lan_ip =~ /\d+\.\d+\.\d+\.(\d+)/; $dmz_dhcp_start = $octet + 1; $dmz_dhcp_end = $dmz_dhcp_start + (1 << $dmz_mode) - 4; $parms{dmz_lan_ip} = $dmz_lan_ip; $parms{dmz_lan_mask} = $dmz_lan_mask; $parms{dmz_dhcp_start} = $dmz_dhcp_start; $parms{dmz_dhcp_end} = $dmz_dhcp_end; } # derive values which are not explicitly defined $parms{dhcp_limit} = $dhcp_limit = $dhcp_end - $dhcp_start + 1; $parms{dmz_dhcp_limit} = $dmz_dhcp_limit = $dmz_dhcp_end - $dmz_dhcp_start + 1; # # get the active wifi settings on a fresh page load # unless($parms{reload}) { my $wifiintf = get_interface("wifi"); ($wifi_txpower) = `iwinfo $wifiintf info 2>/dev/null` =~ /Tx-Power: (\d+)/; (my $doesiwoffset) = `iwinfo $wifiintf info 2>/dev/null` =~ /TX power offset: (\d+)/; if ( $doesiwoffset ) { $wifi_txpower -= $1; } $slottime = ""; } # sanitize the active settings $wifi_txpower = wifi_maxpower($wifi_channel) if not defined $wifi_txpower or $wifi_txpower > wifi_maxpower($wifi_channel); $wifi_txpower = 1 if $wifi_txpower < 1; $wifi_distance = 0 unless defined $wifi_distance; $wifi_distance = 0 if $wifi_distance =~ /\D/; # stuff the sanitized data back into the parms hash # so they get saved correctly $parms{wifi_distance} = $wifi_distance; $parms{wifi_txpower} = $wifi_txpower; # # apply the wifi settings # if($parms{button_apply} or $parms{button_save}) { if($wifi_distance < 1 or $wifi_distance =~ /\D/) { push (@errors, "invalid distance value"); } else { my $wifiintf = get_interface("wifi"); $cmd = ""; $cmd .= "iw phy phy0 set distance $wifi_distance >/dev/null 2>&1;"; $cmd .= "iw dev $wifiintf set txpower fixed ${wifi_txpower}00 >/dev/null 2>&1;"; system $cmd; } } if($parms{button_updatelocation}) { # Process gridsquare ----------------------------------- if($parms{gridsquare}) { # validate values if($parms{gridsquare} =~ /^[A-Z][A-Z]\d\d[a-z][a-z]$/) { # delete/define file unlink("/etc/gridsquare") if(-f "/etc/gridsquare"); my $rcgood=open(my $gs, ">", "/etc/gridsquare"); push @errors, "Cannot open gridsquare file" unless $rcgood; print $gs "$parms{gridsquare}\n"; close($gs); push @output, "Gridsquare updated.\n"; } else { push @errors, "ERROR: Gridsquare format is: 2-uppercase letters, 2-digits, 2-lowercase letters. (AB12cd)\n"; } } else { unlink("/etc/gridsquare") if(-f "/etc/gridsquare"); push @output, "Gridsquare purged.\n"; } # Process LAT/LNG --------------------------------------------- if($parms{latitude} and $parms{longitude}) { # validate values if($parms{latitude} =~ /^([-+]?\d{1,2}([.]\d+)?)$/ and $parms{longitude} =~ /^([-+]?\d{1,3}([.]\d+)?)$/) { # delete/define file unlink("/etc/latlon") if(-f "/etc/latlon"); $rcgood=open(my $ll, ">", "/etc/latlon"); push @errors, "Cannot open lat/lon file" unless $rcgood; print $ll "$parms{latitude}\n"; print $ll "$parms{longitude}\n"; close($ll); push @output, "Lat/lon updated.\n"; } else { push @errors, "ERROR: Lat/lon format is decimal: (ex. 30.121456 or -95.911154)\n"; } } else { unlink("/etc/latlon") if(-f "/etc/latlon"); push @output, "Lat/lon purged.\n"; } } # # retrieve location data # if(-f "/etc/latlon") { $rcgood=open(FILE, "/etc/latlon"); push @errors, "ERROR: reading lat/lon data\n" unless $rcgood; while(){ chomp; push @lines,$_; } close(FILE); $lat=$lines[0]; $lon=$lines[1]; } @lines=(); if(-f "/etc/gridsquare") { $rcgood=open(FILE, "/etc/gridsquare"); push @errors, "ERROR: reading gridsquare data\n" unless $rcgood; while(){ chomp; push @lines,$_; } close(FILE); $gridsquare=$lines[0]; } # validate and save configuration if($parms{button_save}) { # lookup the tz string for the selected time_zone $time_zone = $$tz_db_strings{$time_zone_name}; $parms{time_zone} = $time_zone; if($wifi_proto eq "static") { if(not validate_netmask($wifi_mask)) { push @errors, "invalid WiFi netmask"; } elsif(not validate_ip_netmask($wifi_ip, $wifi_mask)) { push @errors, "invalid WiFi IP address"; } } if ($config eq "mesh"){ push (@errors, "invalid WiFi SSID") unless length $wifi_ssid <= 27; } else { push (@errors, "invalid WiFi SSID") unless length $wifi_ssid <= 32; } if ( is_channel_valid($wifi_channel) != 1 ) { push (@errors, "invalid WiFi channel") } if ( !is_wifi_chanbw_valid($wifi_chanbw,$wifi_ssid) ) { push (@errors, "Invalid WiFi channel width"); $wifi_chanbw = 20; } $wifi_country_validated=0; foreach my $testcountry (split(',',"00,HX,AD,AE,AL,AM,AN,AR,AT,AU,AW,AZ,BA,BB,BD,BE,BG,BH,BL,BN,BO,BR,BY,BZ,CA,CH,CL,CN,CO,CR,CY,CZ,DE,DK,DO,DZ,EC,EE,EG,ES,FI,FR,GE,GB,GD,GR,GL,GT,GU,HN,HK,HR,HT,HU,ID,IE,IL,IN,IS,IR,IT,JM,JP,JO,KE,KH,KP,KR,KW,KZ,LB,LI,LK,LT,LU,LV,MC,MA,MO,MK,MT,MY,MX,NL,NO,NP,NZ,OM,PA,PE,PG,PH,PK,PL,PT,PR,QA,RO,RS,RU,RW,SA,SE,SG,SI,SK,SV,SY,TW,TH,TT,TN,TR,UA,US,UY,UZ,VE,VN,YE,ZA,ZW")) { if ( $testcountry eq $wifi_country ) { $wifi_country_validated=1; break; } } if ( $wifi_country_validated ne 1 ) { $wifi_country="00"; push (@errors, "Invalid country"); } if($lan_proto eq "static") { if(not validate_netmask($lan_mask)) { push @errors, "invalid LAN netmask"; } elsif($lan_mask !~ /^255\.255\.255\./) { push @errors, "LAN netmask must begin with 255.255.255"; } elsif(not validate_ip_netmask($lan_ip, $lan_mask)) { push @errors, "invalid LAN IP address"; } else { if($lan_dhcp) { my $start_addr = change_ip_address($lan_ip, $dhcp_start); my $end_addr = change_ip_address($lan_ip, $dhcp_end); unless(validate_ip_netmask($start_addr, $lan_mask) and validate_same_subnet($start_addr, $lan_ip, $lan_mask)) { push @errors, "invalid DHCP start address"; } unless(validate_ip_netmask($end_addr, $lan_mask) and validate_same_subnet($end_addr, $lan_ip, $lan_mask)) { push @errors, "invalid DHCP end address"; } if($dhcp_start > $dhcp_end) { push @errors, "invalid DHCP start/end addresses"; } } if($lan_gw and not (validate_ip_netmask($lan_gw, $lan_mask) and validate_same_subnet($lan_ip, $lan_gw, $lan_mask))) { push @errors, "invalid LAN gateway"; } } } if($wan_proto eq "static") { if(not validate_netmask($wan_mask)) { push @errors, "invalid WAN netmask"; } elsif(not validate_ip_netmask($wan_ip, $wan_mask)) { push @errors, "invalid WAN IP address"; } else { unless (validate_ip_netmask($wan_gw, $wan_mask) and validate_same_subnet($wan_ip, $wan_gw, $wan_mask)) { push @errors, "invalid WAN gateway"; } } } push (@errors, "invalid WAN DNS 1") unless validate_ip($wan_dns1); push (@errors, "invalid WAN DNS 2") if $wan_dns2 ne "" and not validate_ip($wan_dns2); if($passwd1 or $passwd2) { push (@errors, "passwords do not match") if $passwd1 ne $passwd2; push (@errors, "passwords cannot contain '#'") if $passwd1 =~ /#/; push (@errors, "password must be changed") if $passwd1 eq "hsmm"; } elsif(-f "/etc/config/unconfigured") { push @errors, "password must be changed during initial configuration"; } if($nodetac =~ /\//) { $nodetac =~ /^\s*([\w\-]+)\s*\/\s*([\w\-]+)\s*$/; $node = $1; $tactical = $2; push(@errors, "invalid node/tactical name") if not $2; } else { $node = $nodetac; $tactical = ""; push(@errors, "you must set the node name") if $node eq ""; } if($node and ($node =~ /[^\w\-]/ or $node =~ /_/)) { push(@errors, "invalid node name"); } if($tactical =~ /[^\w\-]/ or $tactical =~ /_/) { push(@errors, "invalid tactical name"); } if($ntp_server eq '' || validate_fqdn($ntp_server) == 0) { push(@errors, "invalid ntp server"); } if($debug == 3) # don't save the config, just validate it { push (@errors, "OK") unless @errors; } unless(@errors) { $parms{node} = $node; $parms{tactical} = $tactical; system "touch /tmp/unconfigured" if -f "/etc/config/unconfigured"; $rc = save_setup("/etc/config.$config/_setup"); if(-s "/tmp/web/save/node-setup.out") { push @errors, `cat /tmp/web/save/node-setup.out`; } elsif(not $rc) { push @errors, "error saving setup"; } reboot_page("/cgi-bin/status") if -f "/tmp/unconfigured" and not @errors; } } system "rm -rf /tmp/web/save"; reboot_page("/cgi-bin/status") if $parms{button_reboot}; # # generate the page # http_header() unless $debug == 2; html_header(nvram_get("node") . " setup", 0); print "\n" if($pingOk); print "\n" if($pingOk); print ""; print "
\n"; print " "; alert_banner(); print "
\n" unless $debug == 2; print "\n" if $debug == 2; print "\n"; print "\n"; # # control buttons # print "\n"; # messages if(@output) { # print "\n"; print "\n"; } if(@errors) { print "\n"; print "\n"; } elsif($parms{button_save}) { print "\n"; } if(not @errors and -f "/tmp/reboot-required") { print ""; } # # node name and type, password # print ""; if($config ne "mesh") { print ""; } print "\n"; # # Optional Settings # print ""; print "
\n"; navbar("setup"); print "
Help          
 
Configuration NOT saved!
\n"; print "
    \n"; foreach(@output) { print "
  • $_
  • \n" } print "
\n"; print "
Configuration NOT saved!
\n"; print "
    \n"; foreach(@errors) { print "
  • $_
  • \n" } print "
\n"; print "
"; print "Configuration saved.

\n"; print "

Reboot is required for changes to take effect

\n"; print ""; if(0)#$config eq "mesh") { print ""; print ""; print "\n"; } print " "; print "
Node Name Password  Latitude
Node Type Verify Password
"; print "This node type will be removed in a future release -- See release notes"; print "

"; print "\n"; # # LAN settings # print "\n"; # # WAN settings # print "
\n"; # # WiFi settings # print "\n\n"; if($wifi_proto eq "static") { print "\n"; print "\n"; print "\n"; print "\n"; } else { push @hidden, ""; push @hidden, ""; } if($wifi_proto ne "disabled") { # Reset wifi channel/bandwidth to default if ( -f "/etc/config/unconfigured" || $parms{button_reset} ) { my $defaultwifi = rf_default_channel(); $wifi_channel = $defaultwifi->{'channel'}; $wifi_chanbw = $defaultwifi->{'chanbw'}; } print "\n"; print "\n"; } else { print "\n"; } if($wifi_mode eq "ap") { print ""; print "\n"; } print "\n"; if($config ne "user") { push @hidden, ""; print "\n"; if($wifi_mode ne "sta") { print "\n"; print "\n"; } else { push @hidden, ""; } { print "\n"; print "\n"; } if ($config ne "mesh") { print "\n"; print "\n"; } else { push (@hidden, ""); } print "\n"; print ""; print "\n"; print "\n"; print "\n"; $wifi_distance_disp=int($wifi_distance/1000); print "\n"; print "\n"; } else { push @hidden, ""; push @hidden, ""; push @hidden, ""; push @hidden, ""; push @hidden, ""; } print "
WiFi
Protocol "; #if($config ne "user" and $config ne "client") if($config ne "user") { push @hidden, ""; print "\n"; } $dis = ""; $dis = "disabled" if $config eq "client"; selopt("Static", "static", $wifi_proto); selopt("DHCP", "dhcp", $wifi_proto); selopt("Bridged", "bridged", $wifi_proto, $dis); selopt("disabled", "disabled", $wifi_proto, $dis); print "
IP Address
Netmask
SSID"; if ($config eq "mesh") { print "-$wifi_chanbw-v3
Hidden
Mode
Channel
Channel Width
Country

Active Settings

Antenna selection is now automatic

Tx Power  
Distance to
FARTHEST Neighbor
 kilometers
"; print "
"; print "Miles "; print "Kilometers
"; print ""; print "
\n\n"; push @hidden, ""; if($dmz_mode) { print ""; #print "\n"; print "\n"; push @hidden, ""; print ""; #print "\n"; print "\n"; push @hidden, ""; print ""; print "\n"; print ""; #print "\n"; print "\n"; push @hidden, ""; print ""; #print "\n"; print "\n"; print ""; print ""; print ""; print "\n"; push @hidden, ""; push @hidden, ""; push @hidden, ""; push @hidden, ""; push @hidden, ""; push @hidden, ""; } else { print ""; print "\n"; print ""; print "\n"; if($wan_proto eq "disabled" and $wifi_proto ne "dhcp") { print ""; print "\n"; } print ""; print "\n"; print ""; print "\n"; print ""; print "\n"; push @hidden, ""; push @hidden, ""; push @hidden, ""; push @hidden, ""; push @hidden, ""; } if($config eq "mesh" and 0) # disable for now { print "\n"; print "\n"; print "\n"; } print "
LAN
LAN Mode
IP Address
$dmz_lan_ip
Netmask
$dmz_lan_mask
DHCP Server
DHCP Start
$dmz_dhcp_start
DHCP End
$dmz_dhcp_end

Advanced
Disable
Default Route
IP Address
Netmask
Gateway
DHCP Server"; } print " checked" if $lan_dhcp; print ">
DHCP Start"; } print ">
DHCP End"; } print ">

Mesh Bridge
\n"; if($config ne "client" and $config ne "mesh_ap") { print "\n\n"; if($wan_proto eq "static") { print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; } else { push @hidden, ""; push @hidden, ""; push @hidden, ""; } print "\n"; print "\n"; print "\n"; print "\n"; if($config eq "mesh") { print "\n"; print ""; print "\n"; print "\n"; } print "
WAN
Protocol"; print "
IP Address
Netmask
Gateway
DNS 1
DNS 2

Advanced
Mesh Gateway

\n"; print ""; print ""; print ""; print "\n"; print ""; print "\n"; print "" if($pingOk); print ""; print ""; print "
Optional Settings

Latitude"; print " "; print ""; if($pingOk) { print "  "; print " "; } else { print "  "; print " "; } print "
LongitudeGrid Square

Timezone NTP Server
\n"; push @hidden, ""; push @hidden, ""; push @hidden, ""; foreach(@hidden) { print "$_\n" } print "
\n"; show_debug_info(); if($debug) { print "
config
\n"; foreach(sort keys %cfg) { $tmp = $cfg{$_}; $tmp =~ s/ /\(space\)/g; if($cfg{$_} eq "") { print "$_ = (null)
\n" } else { print "$_ = $tmp
\n" } } } show_parse_errors(); page_footer(); if($pingOk) { print < var map = L.map('map').setView([0.0, 0.0], 1); L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiazVkbHEiLCJhIjoiY2lqMnlieTM4MDAyNXUwa3A2eHMxdXE3MiJ9.BRFvx4q2vi70z5Uu2zRYQw', { maxZoom: 18, attribution: 'Map data © OpenStreetMap contributors, ' + 'CC-BY-SA, ' + 'Imagery ©Mapbox', id: 'mapbox.streets' }).addTo(map); var marker; function onMapClick(e) { marker= new L.marker(e.latlng,{draggable: true}); map.addLayer(marker); document.getElementsByName('latitude')[0].value=e.latlng.lat.toFixed(6).toString(); document.getElementsByName('longitude')[0].value=e.latlng.lng.toFixed(6).toString(); map.off('click', onMapClick); marker.on('drag', onMarkerDrag); } EOF if($lat and $lon) { print "marker= new L.marker([$lat,$lon],{draggable: true});"; print "map.addLayer(marker);"; print "map.setView([$lat,$lon],13);"; print "marker.on('drag', onMarkerDrag);"; } else { print "map.on('click', onMapClick);"; } print < EOF } print "\n"; print "\n";