controller.php 8.0 KB

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