controller.php 6.8 KB

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