diff options
Diffstat (limited to 'maj/gen-dev.php')
-rw-r--r-- | maj/gen-dev.php | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/maj/gen-dev.php b/maj/gen-dev.php new file mode 100644 index 0000000..5226813 --- /dev/null +++ b/maj/gen-dev.php @@ -0,0 +1,162 @@ +<?php +/** + * Copyright 2016 Ludovic Pouzenc <ludovic@pouzenc.fr> + * + * CHD OpenWRT 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 OpenWRT 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 OpenWRT. If not, see <http://www.gnu.org/licenses/>. +**/ + +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); + +switch ($_POST['profile']) { + case 'TLWR740': + switch($_POST['revision']) { + case '4': + $profile='TLWR740'; + $revision='4'; + $user_filename='mise-a-jour-routeur-TLWR740v4.bin'; + $gen_filename='bin/ar71xx/openwrt-15.05-ar71xx-generic-tl-wr740n-v4-squashfs-factory.bin'; + break; + default: + $errors['revision'] = 'Routeur non supporté (' . (isset($_POST['revision'])?$_POST[$index]:'vide') . ')'; + $res = false; + } + break; + case 'TLWR841': + switch($_POST['revision']) { + case '9': + $profile='TLWR841'; + $revision='9'; + //$user_filename='wr841nv9_wr841ndv9_en_3_16_9_up(150310).bin'; + $user_filename='mise-a-jour-routeur-TLWR841v9.bin'; + $gen_filename='bin/ar71xx/openwrt-15.05-ar71xx-generic-tl-wr841n-v9-squashfs-factory.bin'; + break; + default: + $errors['revision'] = 'Routeur non supporté (' . (isset($_POST['revision'])?$_POST[$index]:'vide') . ')'; + $res = false; + } + break; + default: + $errors['profile'] = 'Routeur non supporté (' . (isset($_POST[$index])?$_POST['profile']:'vide') . ')'; + $res = false; + +} + +if ( !$res ) { + echo "<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"utf-8\" />\n</head>\n<body>\n<h2>Oops, une erreur s'est produite</h2><div>Il y a un problème avec les renseignements fournis<br><ul>\n"; + foreach ($errors as $k=>$v) { + echo "<li>$v</li>\n"; + } + echo '</ul><input action="action" type="button" value="Revenir au formulaire" onclick="history.go(-1);" />'."</div>\n</body>\n</html>\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 <profile> <ip4pub> <lanip4addr> <ip6prefix> <root_passwd> <wireless_ssid> <wireless_key> <wireless_txpower> [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(); +} + +putenv("CACHE_DIR=$basepath/"); +putenv("BUILD_DIR=$basepath/build/"); +exec($command, $output, $return_val); + +// 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 ) { + header('Content-Type: application/octet-stream'); + header("Content-Disposition: attachment; filename=$user_filename"); + readfile("$basepath/build/$gen_filename"); +} else { + echo "<pre>\n$command\n\n"; + echo join($output, "\n") . "\n\n"; + echo "Command return value : "; var_dump($return_val); +} + +fclose($fh); + |