controller.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /* Wifi Hotspot app for YunoHost
  3. * Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  4. * Contribute at https://github.com/labriqueinternet/hotspot_ynh
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. function ynh_setting_get($setting) {
  20. $value = exec("sudo grep \"^$setting:\" /etc/yunohost/apps/hotspot/settings.yml");
  21. $value = preg_replace('/^[^:]+:\s*["\']?/', '', $value);
  22. $value = preg_replace('/\s*["\']$/', '', $value);
  23. return htmlspecialchars($value);
  24. }
  25. function ynh_setting_set($setting, $value) {
  26. return exec('sudo yunohost app setting hotspot '.escapeshellarg($setting).' -v '.escapeshellarg($value));
  27. }
  28. function stop_service() {
  29. exec('sudo systemctl stop ynh-hotspot');
  30. }
  31. function start_service() {
  32. exec('sudo systemctl start ynh-hotspot', $output, $retcode);
  33. return $retcode;
  34. }
  35. function service_status() {
  36. exec('sudo ynh-hotspot status', $output);
  37. return $output;
  38. }
  39. function service_faststatus() {
  40. exec('sudo systemctl is-active hostapd', $output, $retcode);
  41. return $retcode;
  42. }
  43. function ipv6_expanded($ip) {
  44. exec('ipv6_expanded '.escapeshellarg($ip), $output);
  45. return $output[0];
  46. }
  47. function ipv6_compressed($ip) {
  48. exec('ipv6_compressed '.escapeshellarg($ip), $output);
  49. return $output[0];
  50. }
  51. function iw_multissid($nic) {
  52. exec('sudo iw_multissid '.escapeshellarg($nic), $output);
  53. return $output[0];
  54. }
  55. function iw_devices() {
  56. exec('sudo iw_devices', $output);
  57. return getArray($output[0]);
  58. }
  59. function getArray($str) {
  60. return explode('|', $str);
  61. }
  62. function noneValue($str) {
  63. return ($str == 'none') ? '' : $str;
  64. }
  65. function is_connected_through_hotspot($ip6_net, $ip4_nat_prefix) {
  66. $ip = $_SERVER['REMOTE_ADDR'];
  67. foreach($ip6_net as $net) {
  68. $ip6_regex = '/^'.preg_quote(preg_replace('/::$/', '', $net)).':/';
  69. if(preg_match($ip6_regex, $ip)) {
  70. return true;
  71. }
  72. }
  73. foreach($ip4_nat_prefix as $prefix) {
  74. $ip4_regex = '/^'.preg_quote($prefix).'\./';
  75. if(preg_match($ip4_regex, $ip)) {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. dispatch('/', function() {
  82. $ssids = array();
  83. $devs = iw_devices();
  84. $devs_list = '';
  85. $wifi_device = ynh_setting_get('wifi_device');
  86. $multissid = ynh_setting_get('multissid');
  87. $wifi_channel = ynh_setting_get('wifi_channel');
  88. foreach($devs AS $dev) {
  89. $dev_multissid = iw_multissid($dev);
  90. $active = ($dev == $wifi_device) ? 'class="active"' : '';
  91. $devs_list .= "<li $active data-multissid='$dev_multissid'><a href='#'>$dev</a></li>\n";
  92. }
  93. $wifi_ssid = getArray(ynh_setting_get('wifi_ssid'));
  94. $wifi_secure = getArray(ynh_setting_get('wifi_secure'));
  95. $wifi_passphrase = getArray(ynh_setting_get('wifi_passphrase'));
  96. $ip6_net = getArray(ynh_setting_get('ip6_net'));
  97. $ip6_firewall = getArray(ynh_setting_get('ip6_firewall'));
  98. $ip6_dns0 = getArray(ynh_setting_get('ip6_dns0'));
  99. $ip6_dns1 = getArray(ynh_setting_get('ip6_dns1'));
  100. $ip4_nat_prefix = getArray(ynh_setting_get('ip4_nat_prefix'));
  101. $ip4_dns0 = getArray(ynh_setting_get('ip4_dns0'));
  102. $ip4_dns1 = getArray(ynh_setting_get('ip4_dns1'));
  103. for($i = 0; $i < $multissid; $i++) {
  104. $ssid = [
  105. 'id' => $i,
  106. 'wifi_ssid' => noneValue($wifi_ssid[$i]),
  107. 'wifi_secure' => noneValue($wifi_secure[$i]),
  108. 'wifi_passphrase' => noneValue($wifi_passphrase[$i]),
  109. 'ip6_net' => noneValue($ip6_net[$i]),
  110. 'ip6_firewall' => noneValue($ip6_firewall[$i]),
  111. 'ip6_dns0' => noneValue($ip6_dns0[$i]),
  112. 'ip6_dns1' => noneValue($ip6_dns1[$i]),
  113. 'ip4_nat_prefix' => noneValue($ip4_nat_prefix[$i]),
  114. 'ip4_dns0' => noneValue($ip4_dns0[$i]),
  115. 'ip4_dns1' => noneValue($ip4_dns1[$i]),
  116. ];
  117. array_push($ssids, $ssid);
  118. }
  119. $ip6_net = ynh_setting_get('ip6_net');
  120. $ip6_net = ($ip6_net == 'none') ? '' : getArray($ip6_net);
  121. $ip4_nat_prefix = getArray(ynh_setting_get('ip4_nat_prefix'));
  122. set('service_enabled', ynh_setting_get('service_enabled'));
  123. set('ssids', $ssids);
  124. set('wifi_device', $wifi_device);
  125. set('wifi_channel', $wifi_channel);
  126. set('wifi_device_list', $devs_list);
  127. set('faststatus', service_faststatus() == 0);
  128. set('is_connected_through_hotspot', is_connected_through_hotspot($ip6_net, $ip4_nat_prefix));
  129. return render('settings.html.php');
  130. });
  131. dispatch_put('/settings', function() {
  132. exec('ip link show '.escapeshellarg($_POST['wifi_device']), $output, $retcode);
  133. $wifi_device_exists = ($retcode == 0);
  134. $service_enabled = isset($_POST['service_enabled']) ? 1 : 0;
  135. $wifi_ssid_uniqueness = array();
  136. $ip4_nat_prefix_uniqueness = array();
  137. $ip6_net_uniqueness = array();
  138. $ssids = array();
  139. $id = 0;
  140. if($service_enabled == 1) {
  141. try {
  142. foreach($_POST['ssid'] as $key => $ssid) {
  143. $id = $key + 1;
  144. $ssid['ip6_net'] = empty($ssid['ip6_net']) ? 'none' : $ssid['ip6_net'];
  145. $ssid['ip6_addr'] = 'none';
  146. $ssid['ip6_firewall'] = isset($ssid['ip6_firewall']) ? 1 : 0;
  147. $ssid['wifi_secure'] = isset($ssid['wifi_secure']) ? 1 : 0;
  148. if(!$ssid['wifi_secure']) {
  149. $ssid['wifi_passphrase'] = 'none';
  150. }
  151. if(in_array($ssid['wifi_ssid'], $wifi_ssid_uniqueness)) {
  152. throw new Exception(_('All Wifi names must be unique'));
  153. } else {
  154. array_push($wifi_ssid_uniqueness, $ssid['wifi_ssid']);
  155. }
  156. if(in_array($ssid['ip4_nat_prefix'], $ip4_nat_prefix_uniqueness)) {
  157. throw new Exception(_('All IPv4 NAT prefixes must be unique'));
  158. } else {
  159. array_push($ip4_nat_prefix_uniqueness, $ssid['ip4_nat_prefix']);
  160. }
  161. if($ssid['ip6_net'] != 'none' && in_array($ssid['ip6_net'], $ip6_net_uniqueness)) {
  162. throw new Exception(_('All IPv6 delegated prefixes must be unique'));
  163. } else {
  164. array_push($ip6_net_uniqueness, $ssid['ip6_net']);
  165. }
  166. if(empty($ssid['wifi_ssid']) || empty($ssid['wifi_passphrase'])) {
  167. throw new Exception(_('Your Wifi Hotspot needs a name and a password'));
  168. }
  169. if($ssid['wifi_secure'] && (strlen($ssid['wifi_passphrase']) < 8 || strlen($ssid['wifi_passphrase']) > 63)) {
  170. throw new Exception(_('Your password must from 8 to 63 characters (WPA2 passphrase)'));
  171. }
  172. if($ssid['wifi_secure'] && preg_match('/[^[:print:]]/', $ssid['wifi_passphrase'])) {
  173. $msg = _('Only <LINK:ASCII>printable ASCII characters</LINK:ASCII> are permitted in your password');
  174. $msg = str_replace('<LINK:ASCII>', '<a href="https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/ASCII_full.svg/217px-ASCII_full.svg.png" class="alert-link">', $msg);
  175. $msg = str_replace('</LINK:ASCII>', '</a>', $msg);
  176. throw new Exception($msg);
  177. }
  178. if(!$wifi_device_exists) {
  179. throw new Exception(_('The wifi antenna interface seems not exist on the system'));
  180. }
  181. if($ssid['ip6_net'] != 'none') {
  182. $ssid['ip6_net'] = ipv6_expanded($ssid['ip6_net']);
  183. if(empty($ssid['ip6_net'])) {
  184. throw new Exception(_('The IPv6 Delegated Prefix format looks bad'));
  185. }
  186. $ip6_blocs = explode(':', $ssid['ip6_net']);
  187. $ssid['ip6_addr'] = "${ip6_blocs[0]}:${ip6_blocs[1]}:${ip6_blocs[2]}:${ip6_blocs[3]}:${ip6_blocs[4]}:${ip6_blocs[5]}:${ip6_blocs[6]}:42";
  188. $ssid['ip6_net'] = ipv6_compressed($ssid['ip6_net']);
  189. $ssid['ip6_addr'] = ipv6_compressed($ssid['ip6_addr']);
  190. }
  191. if(!empty($ssid['ip6_dns0'])) {
  192. $ssid['ip6_dns0'] = ipv6_expanded($ssid['ip6_dns0']);
  193. if(empty($ssid['ip6_dns0'])) {
  194. throw new Exception(_('The format of the first IPv6 DNS Resolver looks bad'));
  195. }
  196. $ssid['ip6_dns0'] = ipv6_compressed($ssid['ip6_dns0']);
  197. if(!empty($ssid['ip6_dns1'])) {
  198. $ssid['ip6_dns1'] = ipv6_expanded($ssid['ip6_dns1']);
  199. if(empty($ssid['ip6_dns1'])) {
  200. throw new Exception(_('The format of the second IPv6 DNS Resolver looks bad'));
  201. }
  202. $ssid['ip6_dns1'] = ipv6_compressed($ssid['ip6_dns1']);
  203. }
  204. }
  205. if(inet_pton($ssid['ip4_dns0']) === false) {
  206. throw new Exception(_('The format of the first IPv4 DNS Resolver looks bad'));
  207. }
  208. if(inet_pton($ssid['ip4_dns1']) === false) {
  209. throw new Exception(_('The format of the second IPv4 DNS Resolver looks bad'));
  210. }
  211. if(inet_pton("${ssid['ip4_nat_prefix']}.0") === false) {
  212. throw new Exception(_('The format of the IPv4 NAT Prefix (/24) looks bad: x.x.x expected'));
  213. }
  214. if(filter_var("${ssid['ip4_nat_prefix']}.0", FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) !== false) {
  215. throw new Exception(_('The IPv4 NAT Prefix must be from a private range'));
  216. }
  217. array_push($ssids, $ssid);
  218. }
  219. } catch(Exception $e) {
  220. flash('error', _('Hotspot')." $id: ".$e->getMessage().' ('._('configuration not updated').').');
  221. goto redirect;
  222. }
  223. }
  224. stop_service();
  225. ynh_setting_set('service_enabled', $service_enabled);
  226. $settings = array();
  227. if($service_enabled == 1) {
  228. foreach($ssids as $ssid) {
  229. foreach($ssid as $setting => $value) {
  230. $settings[$setting] .= "$value|";
  231. }
  232. }
  233. ynh_setting_set('multissid', count($ssids));
  234. ynh_setting_set('wifi_device', $_POST['wifi_device']);
  235. ynh_setting_set('wifi_channel', $_POST['wifi_channel']);
  236. foreach($settings as $setting => $value) {
  237. ynh_setting_set($setting, preg_replace('/\|$/', '', $value));
  238. }
  239. $retcode = start_service();
  240. if($retcode == 0) {
  241. flash('success', _('Configuration updated and service successfully reloaded'));
  242. } else {
  243. flash('error', _('Configuration updated but service reload failed'));
  244. }
  245. } else {
  246. flash('success', _('Service successfully disabled'));
  247. }
  248. redirect:
  249. redirect_to('/');
  250. });
  251. dispatch('/status', function() {
  252. $status_lines = service_status();
  253. $status_list = '';
  254. foreach($status_lines AS $status_line) {
  255. if(preg_match('/^\[INFO\]/', $status_line)) {
  256. $status_list .= '<li class="status-info">'.htmlspecialchars($status_line).'</li>';
  257. }
  258. elseif(preg_match('/^\[OK\]/', $status_line)) {
  259. $status_list .= '<li class="status-success">'.htmlspecialchars($status_line).'</li>';
  260. }
  261. elseif(preg_match('/^\[WARN\]/', $status_line)) {
  262. $status_list .= '<li class="status-warning">'.htmlspecialchars($status_line).'</li>';
  263. }
  264. elseif(preg_match('/^\[ERR\]/', $status_line)) {
  265. $status_list .= '<li class="status-danger">'.htmlspecialchars($status_line).'</li>';
  266. }
  267. }
  268. echo $status_list;
  269. });
  270. dispatch('/lang/:locale', function($locale = 'en') {
  271. switch($locale) {
  272. case 'fr':
  273. $_SESSION['locale'] = 'fr';
  274. break;
  275. default:
  276. $_SESSION['locale'] = 'en';
  277. }
  278. redirect_to('/');
  279. });