* Copyright 2017 Nicolas Goaziou * * CHD LEDE 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, either version 3 of the License, or * (at your option) any later version. * * CHD LEDE 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 CHD LEDE. If not, see . **/ function validate($regex, $index, $message, &$errors) { if (isset($_POST[$index]) && 1 === preg_match($regex, $_POST[$index])) { return true; } if (is_array($errors)) $errors[$index] = $message . ' (' . (isset($_POST[$index])?$_POST[$index]:'vide') . ')'; return false; } function checksame($index1, $index2, $message, &$errors) { if ( isset($_POST[$index1]) && isset($_POST[$index2]) && ($_POST[$index1] === $_POST[$index2]) ) { return true; } $errors[$index] = $message; return false; } $pattern_ipv4_optionnal='/(^|((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4})$/'; $pattern_ipv4='/((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$/'; $pattern_ipv6='/^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})\z/i'; $pattern_pass6='/^[^\']{6,64}$/u'; $pattern_pass8='/^[^\']{8,64}$/u'; $pattern_ssid='/^[\w._-]{1,32}$/'; $pattern_iden='/^[\w._-]*$/'; $pattern_rev='/^\d[\d.]*$/'; $pattern_int='/^-?\d+$/'; $errors=array(); $ignore=NULL; $res = validate($pattern_iden, 'profile', 'Profil matériel du routeur incorrect', $errors); $res &= validate($pattern_rev, 'revision', 'Révision matérielle du routeur incorrecte', $errors); $res &= validate($pattern_ipv4, 'ip4pub', 'Adresse IPv4 publique du routeur incorrecte', $errors); $res &= validate($pattern_ipv4_optionnal, 'ip4wan', 'Adresse IPv4 WAN du routeur incorrecte', $errors); $res &= validate($pattern_ipv4, 'ip4lan', 'Adresse IPv4 LAN du routeur incorrecte', $errors); $res &= validate($pattern_ipv6, 'ip6prefix', 'Préfixe IPv6 du routeur incorrect', $errors); //$res &= validate($pattern_ipv6, 'ip6wanll', 'Adresse IPv6 WAN Link Local incorrecte', $errors); $res &= validate($pattern_pass6, 'pwd1', 'Mot de passe du routeur incorrect', $errors); $res &= validate($pattern_pass6, 'pwd2', 'Mot de passe du routeur incorrect', $ignore); $res &= validate($pattern_ssid, 'ssid', 'Nom du réseau WiFi (SSID) incorrect', $errors); $res &= validate($pattern_pass8, 'key1', 'Mot de passe du WiFi incorrect', $errors); $res &= validate($pattern_pass8, 'key2', 'Mot de passe du WiFi incorrect', $ignore); $res &= validate($pattern_int, 'txpower', 'Puissance d\'émission WiFi incorrecte', $errors); $res &= checksame('pwd1', 'pwd2', 'Les mots de passes ne correspondent pas', $errors); $res &= checksame('key1', 'key2', 'Les mots de passes ne correspondent pas', $errors); $region=""; switch ($_POST['profile']) { case 'TLWR740': switch($_POST['revision']) { case '51': $target="tl-wr740n-v5"; break; case '4': case '5': /* case '6': # PAS TESTE, on en a pas */ $target="tl-wr740n-v" . $_POST['revision']; break; default: $errors['revision'] = 'Pas de mise à jour disponible pour l\'instant (' . (isset($_POST['revision'])?$_POST[$index]:'vide') . ')'; $res = false; } break; case 'TLWR841': switch($_POST['revision']) { case '9': case '10': $target="tl-wr841-v" . $_POST['revision']; break; case '11': $target="tl-wr841-v" . $_POST['revision']; $region="-eu"; break; default: $errors['revision'] = 'Pas de mise à jour disponible pour l\'instant (' . (isset($_POST['revision'])?$_POST[$index]:'vide') . ')'; $res = false; } break; default: $errors['profile'] = 'Pas de mise à jour disponible pour l\'instant (' . (isset($_POST[$index])?$_POST['profile']:'vide') . ')'; $res = false; } $profile="DEVICE_$target"; $user_filename="mise-a-jour-routeur-$target.bin"; $gen_fileglob="bin/targets/ar71xx/generic/lede-*-ar71xx-generic-$target-squashfs-factory$region.bin"; if ( !$res ) { echo "\n\n\n\n\n\n

Oops, une erreur s'est produite

Il y a un problème avec les renseignements fournis
    \n"; foreach ($errors as $k=>$v) { echo "
  • $v
  • \n"; } echo '
'."
\n\n\n"; exit(); } // Prevent escapeshellarg to eat UTF-8 non ASCII chars setlocale(LC_CTYPE, "fr_FR.UTF-8"); $basepath='/var/cache/build-openwrt-dev'; $scriptfile='build-openwrt-dev.sh'; $lockfile='/run/lock/build-openwrt-dev.lock'; // Escape everything and build command-line $ip4pub=escapeshellarg($_POST['ip4pub']); $lanip4addr=escapeshellarg($_POST['ip4lan']); $ip6prefix=escapeshellarg($_POST['ip6prefix']); //$wanip6ll=escapeshellarg($_POST['ip6wanll']); $root_passwd=escapeshellarg($_POST['pwd1']); $wireless_ssid=escapeshellarg($_POST['ssid']); $wireless_key=escapeshellarg($_POST['key1']); $wireless_txpower=escapeshellarg($_POST['txpower']); $wanip4addr=escapeshellarg($_POST['ip4wan']); # build-openwrt [ip4wan] $command=escapeshellcmd($scriptfile); $command .= " $profile $ip4pub $lanip4addr $ip6prefix $root_passwd $wireless_ssid $wireless_key $wireless_txpower $wanip4addr 2>&1"; // Prevent multiple parallel executions of the generation part $fh=fopen($lockfile, 'w+'); if ( ! is_resource($fh) ) { echo "PROBLEM opening lock file\n"; exit(); } if ( ! flock($fh, LOCK_EX) ) { echo "PROBLEM acquiring lock file\n"; exit(); } // Run the imagebuilder make command putenv("CACHE_DIR=$basepath/"); putenv("BUILD_DIR=$basepath/build/"); exec($command, $output, $return_val); // Match produced file against expected filename $gen_matchedfiles = glob("$basepath/build/$gen_fileglob"); // Dump script output in /tmp for troobleshooting file_put_contents(tempnam('/tmp' , $scriptfile.'_'), array($command, "\n", print_r($output,true), "\n",$return_val,"\n")); if ( $return_val === 0 && count($gen_matchedfiles) === 1) { header('Content-Type: application/octet-stream'); header("Content-Disposition: attachment; filename=$user_filename"); readfile($gen_matchedfiles[0]); } else { echo "
\n$command\n\n";
	echo join($output, "\n") . "\n\n";
	echo "Expected file pattern : '$basepath/build/$gen_fileglob'"; echo "\n";
	echo "Matched files : "; print_r($gen_matchedfiles); echo "\n";
	echo "Command return value : "; var_dump($return_val); echo "\n";
}

fclose($fh);