controller.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /* VPN Client app for YunoHost
  3. * Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  4. * Contribute at https://github.com/labriqueinternet/vpnclient_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/vpnclient/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 vpnclient '.escapeshellarg($setting).' -v '.escapeshellarg($value));
  27. }
  28. function stop_service() {
  29. exec('sudo systemctl stop ynh-vpnclient');
  30. }
  31. function start_service() {
  32. exec('sudo systemctl start ynh-vpnclient', $output, $retcode);
  33. return $retcode;
  34. }
  35. function service_status() {
  36. exec('sudo ynh-vpnclient status', $output);
  37. return $output;
  38. }
  39. function service_faststatus() {
  40. exec('ip link show tun0', $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. dispatch('/', function() {
  52. $ip6_net = ynh_setting_get('ip6_net');
  53. $ip6_net = ($ip6_net == 'none') ? '' : $ip6_net;
  54. $raw_openvpn = file_get_contents('/etc/openvpn/client.conf.tpl');
  55. set('service_enabled', ynh_setting_get('service_enabled'));
  56. set('server_name', ynh_setting_get('server_name'));
  57. set('server_port', ynh_setting_get('server_port'));
  58. set('server_proto', ynh_setting_get('server_proto'));
  59. set('login_user', ynh_setting_get('login_user'));
  60. set('login_passphrase', ynh_setting_get('login_passphrase'));
  61. set('ip6_net', $ip6_net);
  62. set('crt_client_exists', file_exists('/etc/openvpn/keys/user.crt'));
  63. set('crt_client_key_exists', file_exists('/etc/openvpn/keys/user.key'));
  64. set('crt_server_ca_exists', file_exists('/etc/openvpn/keys/ca-server.crt'));
  65. set('faststatus', service_faststatus() == 0);
  66. set('raw_openvpn', $raw_openvpn);
  67. set('dns0', ynh_setting_get('dns0'));
  68. set('dns1', ynh_setting_get('dns1'));
  69. return render('settings.html.php');
  70. });
  71. dispatch_put('/settings', function() {
  72. $crt_client_exists = file_exists('/etc/openvpn/keys/user.crt');
  73. $crt_client_key_exists = file_exists('/etc/openvpn/keys/user.key');
  74. $crt_server_ca_exists = file_exists('/etc/openvpn/keys/ca-server.crt');
  75. $service_enabled = isset($_POST['service_enabled']) ? 1 : 0;
  76. $ip6_net = empty($_POST['ip6_net']) ? 'none' : $_POST['ip6_net'];
  77. $ip6_addr = 'none';
  78. if($service_enabled == 1) {
  79. try {
  80. if(empty($_POST['server_name']) || empty($_POST['server_port']) || empty($_POST['server_proto'])) {
  81. throw new Exception(_('The Server Address, the Server Port and the Protocol cannot be empty'));
  82. }
  83. if(!preg_match('/^\d+$/', $_POST['server_port'])) {
  84. throw new Exception(_('The Server Port must be only composed of digits'));
  85. }
  86. if($_POST['server_proto'] != 'udp' && $_POST['server_proto'] != 'tcp') {
  87. throw new Exception(_('The Protocol must be "udp" or "tcp"'));
  88. }
  89. if(empty($_POST['dns0']) || empty($_POST['dns1'])) {
  90. throw new Exception(_('You need to define two DNS resolver addresses'));
  91. }
  92. if(($_FILES['crt_client']['error'] == UPLOAD_ERR_OK && $_FILES['crt_client_key']['error'] != UPLOAD_ERR_OK && (!$crt_client_key_exists || $_POST['crt_client_key_delete'] == 1))
  93. || ($_FILES['crt_client_key']['error'] == UPLOAD_ERR_OK && $_FILES['crt_client']['error'] != UPLOAD_ERR_OK && (!$crt_client_exists || $_POST['crt_client_delete'] == 1))) {
  94. throw new Exception(_('A Client Certificate is needed when you suggest a Key, or vice versa'));
  95. }
  96. if(empty($_POST['login_user']) xor empty($_POST['login_passphrase'])) {
  97. throw new Exception(_('A Password is needed when you suggest a Username, or vice versa'));
  98. }
  99. if($_FILES['crt_server_ca']['error'] != UPLOAD_ERR_OK && !$crt_server_ca_exists) {
  100. throw new Exception(_('You need a Server CA.'));
  101. }
  102. if(($_FILES['crt_client_key']['error'] != UPLOAD_ERR_OK && (!$crt_client_key_exists || $_POST['crt_client_key_delete'] == 1)) && empty($_POST['login_user'])) {
  103. throw new Exception(_('You need either a Client Certificate, either a Username, or both'));
  104. }
  105. if($ip6_net != 'none') {
  106. $ip6_net = ipv6_expanded($ip6_net);
  107. if(empty($ip6_net)) {
  108. throw new Exception(_('The IPv6 Delegated Prefix format looks bad'));
  109. }
  110. $ip6_blocs = explode(':', $ip6_net);
  111. $ip6_addr = "${ip6_blocs[0]}:${ip6_blocs[1]}:${ip6_blocs[2]}:${ip6_blocs[3]}:${ip6_blocs[4]}:${ip6_blocs[5]}:${ip6_blocs[6]}:42";
  112. $ip6_net = ipv6_compressed($ip6_net);
  113. $ip6_addr = ipv6_compressed($ip6_addr);
  114. }
  115. } catch(Exception $e) {
  116. flash('error', $e->getMessage().' ('._('configuration not updated').').');
  117. goto redirect;
  118. }
  119. }
  120. stop_service();
  121. ynh_setting_set('service_enabled', $service_enabled);
  122. if($service_enabled == 1) {
  123. ynh_setting_set('server_name', $_POST['server_name']);
  124. ynh_setting_set('server_port', $_POST['server_port']);
  125. ynh_setting_set('server_proto', $_POST['server_proto']);
  126. ynh_setting_set('dns0', $_POST['dns0']);
  127. ynh_setting_set('dns1', $_POST['dns1']);
  128. ynh_setting_set('login_user', $_POST['login_user']);
  129. ynh_setting_set('login_passphrase', $_POST['login_passphrase']);
  130. ynh_setting_set('ip6_net', $ip6_net);
  131. ynh_setting_set('ip6_addr', $ip6_addr);
  132. file_put_contents('/etc/openvpn/client.conf.tpl', $_POST['raw_openvpn']);
  133. if($_FILES['crt_client']['error'] == UPLOAD_ERR_OK) {
  134. move_uploaded_file($_FILES['crt_client']['tmp_name'], '/etc/openvpn/keys/user.crt');
  135. } elseif($_POST['crt_client_delete'] == 1) {
  136. unlink('/etc/openvpn/keys/user.crt');
  137. }
  138. if($_FILES['crt_client_key']['error'] == UPLOAD_ERR_OK) {
  139. move_uploaded_file($_FILES['crt_client_key']['tmp_name'], '/etc/openvpn/keys/user.key');
  140. } elseif($_POST['crt_client_key_delete'] == 1) {
  141. unlink('/etc/openvpn/keys/user.key');
  142. }
  143. if($_FILES['crt_server_ca']['error'] == UPLOAD_ERR_OK) {
  144. move_uploaded_file($_FILES['crt_server_ca']['tmp_name'], '/etc/openvpn/keys/ca-server.crt');
  145. }
  146. if(!empty($_POST['login_user'])) {
  147. file_put_contents('/etc/openvpn/keys/credentials', "${_POST['login_user']}\n${_POST['login_passphrase']}");
  148. } else {
  149. file_put_contents('/etc/openvpn/keys/credentials', '');
  150. }
  151. $retcode = start_service();
  152. if($retcode == 0) {
  153. flash('success', _('Configuration updated and service successfully reloaded'));
  154. } else {
  155. flash('error', _('Configuration updated but service reload failed'));
  156. }
  157. } else {
  158. flash('success', _('Service successfully disabled'));
  159. }
  160. redirect:
  161. redirect_to('/');
  162. });
  163. dispatch('/status', function() {
  164. $status_lines = service_status();
  165. $status_list = '';
  166. foreach($status_lines AS $status_line) {
  167. if(preg_match('/^\[INFO\]/', $status_line)) {
  168. $status_list .= '<li class="status-info">'.htmlspecialchars($status_line).'</li>';
  169. }
  170. elseif(preg_match('/^\[OK\]/', $status_line)) {
  171. $status_list .= '<li class="status-success">'.htmlspecialchars($status_line).'</li>';
  172. }
  173. elseif(preg_match('/^\[WARN\]/', $status_line)) {
  174. $status_list .= '<li class="status-warning">'.htmlspecialchars($status_line).'</li>';
  175. }
  176. elseif(preg_match('/^\[ERR\]/', $status_line)) {
  177. $status_list .= '<li class="status-danger">'.htmlspecialchars($status_line).'</li>';
  178. }
  179. }
  180. echo $status_list;
  181. });
  182. dispatch('/lang/:locale', function($locale = 'en') {
  183. switch($locale) {
  184. case 'fr':
  185. $_SESSION['locale'] = 'fr';
  186. break;
  187. default:
  188. $_SESSION['locale'] = 'en';
  189. }
  190. redirect_to('/');
  191. });