#!/usr/bin/perl
=for comment
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 contained 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
BEGIN {push @INC, '/www/cgi-bin'};
use perlfunc;
sub firmware_list_gen
{
@fw_images = ();
chomp($fw_version = `cat /etc/mesh-release`);
foreach(`cat /tmp/web/firmware.list 2>/dev/null`)
{
my($md5, $fw, $tag) = /^(\S+) (\S+) (.*)/;
next unless $tag;
next if $tag eq "none";
next unless ($tag eq "all") or ($tag eq "dev" and $parms{dev}) or ($fw_version =~ /$tag/);
push @fw_images, $fw;
$fw_md5{$fw} = $md5;
}
}
$debug = 0;
$| = 1;
$tunnel_active= 0;
if ( -e "/usr/sbin/vtund" && open(my $tuncfgfd, '/etc/config/vtun')) {
while ( my $line = <$tuncfgfd> ) {
if ( $line =~ /option enabled '1'/i ) {
$tunnel_active = 1;
last;
}
}
}
if ( $tunnel_active ) {
read_postdata({acceptfile => false});
} else {
read_postdata({acceptfile => true});
}
reboot_page("/cgi-bin/status") if $parms{button_reboot};
read_query_string();
$node = nvram_get("node");
$tmpdir = "/tmp/web/admin";
system "mkdir -p $tmpdir";
# make developer mode stick
system "touch /tmp/developer_mode" if $parms{dev};
$parms{dev} = 1 if -e "/tmp/developer_mode";
# set the wget command options
$wget = "wget -U 'node: $node'";
#
# handle firmware updates
#
$fw_install = 0;
$patch_install = 0;
@fw_output = ();
@fw_images = ();
%fw_md5 = ();
@serverpaths = ();
$uciserverpath=`uci get aredn.\@downloads[0].firmwarepath`;
chomp($uciserverpath);
push @serverpaths, $uciserverpath;
$hardwaretype = `/usr/local/bin/get_hardwaretype`;
chomp($hardwaretype);
# handle TPLink and Mikrotik exception conditions
$mfg=`/usr/local/bin/get_hardware_mfg`;
chomp($mfg);
$mfgprefix="";
if($mfg=~ /Ubiquiti/i)
{
$mfgprefix="ubnt";
} elsif($mfg=~ /Mikrotik/i) {
$mfgprefix="mikrotik";
} elsif($mfg=~ /TP-Link/i) {
$mfgprefix="cpe";
}
if($hardwaretype eq "nanostation-m")
{
$hardwaretypev="nano-m";
} elsif($hardwaretype eq "rb-952ui-5ac2nd") {
$hardwaretypev= "rb-nor-flash-16M-ac" ; # hAP AC Lite
} elsif($hardwaretype =~ /rb-912uag-[25]hpnd/i ) {
$hardwaretypev= "nand-large" ; # Basebox 2/5
} elsif($mfgprefix eq "cpe" ) {
$hwmodel = `/usr/local/bin/get_model`;
chomp($hwmodel);
if($hwmodel =~ /CPE210 v1\.[01]/i)
{
$hardwaretypev= "210-220-v1" ; # v1.0/v1.1
} elsif($hwmodel =~ /CPE210 v2\.0/i) {
$hardwaretypev= "210-v2" ; # v2.0
} elsif($hwmodel =~ /CPE210 v3\.0/i) {
$hardwaretypev= "210-v3" ; # v3.0
} elsif($hwmodel =~ /CPE220 v3\.0/i) {
$hardwaretypev= "220-v3" ; # v3.0
} elsif($hwmodel =~ /CPE510 v2\.0/i) {
$hardwaretypev= "510-v2" ; # v2.0
} elsif($hwmodel =~ /CPE510 v3\.0/i) {
$hardwaretypev= "510-v3" ; # v3.0
} elsif($hwmodel =~ /CPE510/i) {
$hardwaretypev= "510-520-v1" ; # CPE510 V1.0/v1.1
} elsif($hwmodel =~ /CPE610/i) {
$hardwaretypev= "610-v1" ; # CPE610 V1.0
}
} else {
$hardwaretypev=$hardwaretype;
}
# refresh fw
if($parms{button_refresh_fw})
{
if(get_default_gw() ne "none")
{
push @fw_output, "Downloading firmware list from $uciserverpath...\n";
unlink "/tmp/web/firmware.list";
$ok = 0;
foreach $serverpath (@serverpaths)
{
system "$wget -O /tmp/web/firmware.list $serverpath/firmware.$hardwaretype.list >/dev/null 2>>$tmpdir/wget.err";
unless($?) { $ok = 1; last }
}
if($ok) { push @fw_output, "Done.\n" }
else { push @fw_output, `cat $tmpdir/wget.err` }
unlink "$tmpdir/wget.err";
}
else
{
push @fw_output, "Error: no route to the Internet\n";
unlink "/tmp/web/firmware.list";
}
}
# generate data structures
# and set $fw_version
firmware_list_gen();
# upload fw
if($parms{button_ul_fw} and -f "/tmp/web/upload/file")
{
system "mv -f /tmp/web/upload/file $tmpdir/firmware";
if($parms{firmfile} =~ /sysupgrade\.bin$/) # full firmware
{
$fw_install = 1;
# drop the page cache to take pressure of tmps when checking the firmware
`echo 3 > /proc/sys/vm/drop_caches`;
# check firmware header
if(system "/usr/local/bin/firmwarecheck.sh $tmpdir/firmware")
{
push @fw_output, "Firmware CANNOT be updated\n";
push @fw_output, "firmware file is not valid\n";
$fw_install = 0;
unlink("$tmpdir/firmware");
system("/usr/local/bin/uploadctlservices","restore") and push @fw_output, "Failed to restart all services, please reboot this node.\n";
}
}
elsif($parms{firmfile} =~ /^patch\S+\.tgz$/) # firmware patch
{
$patch_install = 1;
}
else
{
push @fw_output, "Firmware CANNOT be updated\n";
push @fw_output, "the uploaded file is not recognized\n";
unlink("$tmpdir/firmware");
system("/usr/local/bin/uploadctlservices","restore") and push @fw_output, "Failed to restart all services, please reboot this node.\n";
}
}
# download fw
if($parms{button_dl_fw} and $parms{dl_fw} ne "default")
{
if(get_default_gw() ne "none")
{
unlink "$tmpdir/firmware";
system("/usr/local/bin/uploadctlservices","upgrade");
$ok = 0;
foreach $serverpath (@serverpaths)
{
system "$wget -O $tmpdir/firmware $serverpath/$parms{dl_fw} >/dev/null 2>>$tmpdir/wget.err";
unless($?) { $ok = 1; last }
}
if($parms{dl_fw} =~ /sysupgrade\.bin$/) # full firmware
{
$fw_install = 1;
unless($ok)
{
push @fw_output, "Downloading firmware image...\n";
push @fw_output, `cat $tmpdir/wget.err`;
}
unlink "$tmpdir/wget.err";
# check md5sum
$fw = $parms{dl_fw};
chdir $tmpdir;
if(system "echo '$fw_md5{$fw} firmware' | md5sum -cs")
{
push @fw_output, "Firmware CANNOT be updated\n";
push @fw_output, "firmware file is not valid\n";
$fw_install = 0;
unlink("$tmpdir/firmware");
system("/usr/local/bin/uploadctlservices","restore") and push @fw_output, "Failed to restart all services, please reboot this node.\n";
}
}
elsif($parms{dl_fw} =~ /^patch\S+\.tgz$/) # firmware patch
{
$patch_install = 1;
unless($ok)
{
push @fw_output, "Downloading patch file...\n";
push @fw_output, `cat $tmpdir/wget.err`;
}
unlink "$tmpdir/wget.err";
# check md5sum
$fw = $parms{dl_fw};
chdir $tmpdir;
if(system "echo '$fw_md5{$fw} firmware' | md5sum -cs")
{
push @fw_output, "Firmware CANNOT be updated\n";
push @fw_output, "patch file is not valid\n";
$patch_install = 0;
unlink("$tmpdir/firmware");
system("/usr/local/bin/uploadctlservices","restore") and push @fw_output, "Failed to restart all services, please reboot this node.\n";
}
}
else
{
push @fw_output, "Firmware CANNOT be updated\n";
push @fw_output, "the downloaded file is not recognized\n";
unlink("$tmpdir/firmware");
system("/usr/local/bin/uploadctlservices","restore") and push @fw_output, "Failed to restart all services, please reboot this node.\n";
}
}
else
{
push @fw_output, "Error: no route to the Internet\n";
unlink "/tmp/web/firmware.list";
}
}
# install fw
if($fw_install and -f "$tmpdir/firmware")
{
my $junk;
http_header();
html_header("FIRMWARE UPDATE IN PROGRESS", 0);
print "";
print "\n";
print "
\n";
print "
The firmware is being updated.
\n";
print "
DO NOT REMOVE POWER UNTIL UPDATE IS FINISHED
\n";
print "
\n";
unless($debug)
{
# drop the page cache to take pressure of tmps for the upgrade process
`echo 3 > /proc/sys/vm/drop_caches`;
`/usr/local/bin/upgrade_kill_prep`;
if ( $parms{checkbox_keep_settings} )
{
print "
Firmware will be written in the background.
If your computer is connected to the LAN of this node you may need to acquire
a new IP address and reset any name service caches you may be using.
The node will reboot twice while the configuration is applied
When the node has finished booting you should ensure your computer has
received a new IP address and reconnect with http://$node.local.mesh:8080/
(This page will automatically reload in 3 minutes)