controller.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. touch('/tmp/.ynh-vpnclient-stopped');
  30. exec('sudo systemctl stop ynh-vpnclient');
  31. }
  32. function start_service() {
  33. exec('sudo systemctl start ynh-vpnclient', $output, $retcode);
  34. unlink('/tmp/.ynh-vpnclient-stopped');
  35. return $retcode;
  36. }
  37. function service_status() {
  38. exec('sudo ynh-vpnclient status', $output);
  39. return $output;
  40. }
  41. function service_faststatus() {
  42. exec('ip link show tun0', $output, $retcode);
  43. return $retcode;
  44. }
  45. function ipv6_expanded($ip) {
  46. exec('ipv6_expanded '.escapeshellarg($ip), $output);
  47. return $output[0];
  48. }
  49. function ipv6_compressed($ip) {
  50. exec('ipv6_compressed '.escapeshellarg($ip), $output);
  51. return $output[0];
  52. }
  53. function noneValue($str) {
  54. return ($str == 'none') ? '' : $str;
  55. }
  56. function readAutoConf($file) {
  57. $json = file_get_contents($file);
  58. $config = json_decode($json, true);
  59. if(!empty($config['crt_server_ca'])) {
  60. $config['crt_server_ca'] = str_replace('|', "\n", $config['crt_server_ca']);
  61. }
  62. if(!empty($config['crt_client'])) {
  63. $config['crt_client'] = str_replace('|', "\n", $config['crt_client']);
  64. }
  65. if(!empty($config['crt_client_key'])) {
  66. $config['crt_client_key'] = str_replace('|', "\n", $config['crt_client_key']);
  67. }
  68. if(!empty($config['crt_client_ta'])) {
  69. $config['crt_client_ta'] = str_replace('|', "\n", $config['crt_client_ta']);
  70. }
  71. return $config;
  72. }
  73. dispatch('/', function() {
  74. $ip6_net = noneValue(ynh_setting_get('ip6_net'));
  75. $raw_openvpn = file_get_contents('/etc/openvpn/client.conf.tpl');
  76. set('service_enabled', ynh_setting_get('service_enabled'));
  77. set('server_name', noneValue(ynh_setting_get('server_name')));
  78. set('server_port', ynh_setting_get('server_port'));
  79. set('server_proto', ynh_setting_get('server_proto'));
  80. set('login_user', ynh_setting_get('login_user'));
  81. set('login_passphrase', ynh_setting_get('login_passphrase'));
  82. set('ip6_net', $ip6_net);
  83. set('crt_client_exists', file_exists('/etc/openvpn/keys/user.crt'));
  84. set('crt_client_key_exists', file_exists('/etc/openvpn/keys/user.key'));
  85. set('crt_client_ta_exists', file_exists('/etc/openvpn/keys/user_ta.key'));
  86. set('crt_server_ca_exists', file_exists('/etc/openvpn/keys/ca-server.crt'));
  87. set('faststatus', service_faststatus() == 0);
  88. set('raw_openvpn', $raw_openvpn);
  89. set('dns0', ynh_setting_get('dns0'));
  90. set('dns1', ynh_setting_get('dns1'));
  91. return render('settings.html.php');
  92. });
  93. dispatch_put('/settings', function() {
  94. if(!isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
  95. throw new Exception('CSRF protection');
  96. }
  97. $service_enabled = isset($_POST['service_enabled']) ? 1 : 0;
  98. if($service_enabled == 1) {
  99. $crt_client_exists = file_exists('/etc/openvpn/keys/user.crt');
  100. $crt_client_key_exists = file_exists('/etc/openvpn/keys/user.key');
  101. $crt_server_ca_exists = file_exists('/etc/openvpn/keys/ca-server.crt');
  102. $config = $_POST;
  103. $autoconf = false;
  104. try {
  105. if($_FILES['cubefile']['error'] == UPLOAD_ERR_OK) {
  106. $config = readAutoConf($_FILES['cubefile']['tmp_name']);
  107. if(is_null($config)) {
  108. throw new Exception(_('Json Syntax Error, please check your dot cube file'));
  109. }
  110. $autoconf = true;
  111. }
  112. $ip6_net = empty($config['ip6_net']) ? 'none' : $config['ip6_net'];
  113. $ip6_addr = 'none';
  114. if(empty($config['server_name']) || empty($config['server_port']) || empty($config['server_proto'])) {
  115. throw new Exception(_('The Server Address, the Server Port and the Protocol cannot be empty'));
  116. }
  117. if(!preg_match('/^\d+$/', $config['server_port'])) {
  118. throw new Exception(_('The Server Port must be only composed of digits'));
  119. }
  120. if($config['server_proto'] != 'udp' && $config['server_proto'] != 'tcp') {
  121. throw new Exception(_('The Protocol must be "udp" or "tcp"'));
  122. }
  123. if(empty($config['dns0']) || empty($config['dns1'])) {
  124. throw new Exception(_('You need to define two DNS resolver addresses'));
  125. }
  126. if(empty($config['login_user']) xor empty($config['login_passphrase'])) {
  127. throw new Exception(_('A Password is needed when you suggest a Username, or vice versa'));
  128. }
  129. if((!$autoconf && (($_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))
  130. || ($_FILES['crt_client_key']['error'] == UPLOAD_ERR_OK && $_FILES['crt_client']['error'] != UPLOAD_ERR_OK && (!$crt_client_exists || $_POST['crt_client_delete'] == 1))))
  131. || ($autoconf && (empty($config['crt_client']) xor empty($config['crt_client_key'])))) {
  132. throw new Exception(_('A Client Certificate is needed when you suggest a Key, or vice versa'));
  133. }
  134. if((!$autoconf && $_FILES['crt_server_ca']['error'] != UPLOAD_ERR_OK && !$crt_server_ca_exists) || ($autoconf && empty($config['crt_server_ca']))) {
  135. throw new Exception(_('You need a Server CA.'));
  136. }
  137. if(((!$autoconf && $_FILES['crt_client_key']['error'] != UPLOAD_ERR_OK && (!$crt_client_key_exists || $_POST['crt_client_key_delete'] == 1)) || ($autoconf && empty($config['crt_client_key']))) && empty($config['login_user'])) {
  138. throw new Exception(_('You need either a Client Certificate, either a Username, or both'));
  139. }
  140. if($ip6_net != 'none') {
  141. $ip6_net = ipv6_expanded($ip6_net);
  142. if(empty($ip6_net)) {
  143. throw new Exception(_('The IPv6 Delegated Prefix format looks bad'));
  144. }
  145. $ip6_blocs = explode(':', $ip6_net);
  146. $ip6_addr = "${ip6_blocs[0]}:${ip6_blocs[1]}:${ip6_blocs[2]}:${ip6_blocs[3]}:${ip6_blocs[4]}:${ip6_blocs[5]}:${ip6_blocs[6]}:42";
  147. $ip6_net = ipv6_compressed($ip6_net);
  148. $ip6_addr = ipv6_compressed($ip6_addr);
  149. }
  150. } catch(Exception $e) {
  151. flash('error', $e->getMessage().' ('._('configuration not updated').').');
  152. goto redirect;
  153. }
  154. }
  155. stop_service();
  156. ynh_setting_set('service_enabled', $service_enabled);
  157. if($service_enabled == 1) {
  158. ynh_setting_set('server_name', $config['server_name']);
  159. ynh_setting_set('server_port', $config['server_port']);
  160. ynh_setting_set('server_proto', $config['server_proto']);
  161. ynh_setting_set('dns0', $config['dns0']);
  162. ynh_setting_set('dns1', $config['dns1']);
  163. ynh_setting_set('login_user', $config['login_user']);
  164. ynh_setting_set('login_passphrase', $config['login_passphrase']);
  165. ynh_setting_set('ip6_net', $ip6_net);
  166. ynh_setting_set('ip6_addr', $ip6_addr);
  167. if($autoconf) {
  168. copy('/etc/openvpn/client.conf.tpl.restore', '/etc/openvpn/client.conf.tpl');
  169. if(!empty($config['openvpn_rm'])) {
  170. $raw_openvpn = explode("\n", file_get_contents('/etc/openvpn/client.conf.tpl'));
  171. $fopenvpn = fopen('/etc/openvpn/client.conf.tpl', 'w');
  172. foreach($raw_openvpn AS $opt) {
  173. $filtered = false;
  174. if(!preg_match('/^#/', $opt) && !preg_match('/<TPL:/', $opt)) {
  175. foreach($config['openvpn_rm'] AS $filter) {
  176. if(!empty($filter) && preg_match("/$filter/i", $opt)) {
  177. $filtered = true;
  178. }
  179. }
  180. }
  181. if(!$filtered) {
  182. fwrite($fopenvpn, "$opt\n");
  183. }
  184. }
  185. fclose($fopenvpn);
  186. }
  187. if(!empty($config['openvpn_add'])) {
  188. $raw_openvpn = file_get_contents('/etc/openvpn/client.conf.tpl');
  189. $raw_openvpn .= "\n# Custom\n".implode("\n", $config['openvpn_add']);
  190. file_put_contents('/etc/openvpn/client.conf.tpl', $raw_openvpn);
  191. }
  192. if(empty($config['crt_client'])) {
  193. if(file_exists('/etc/openvpn/keys/user.crt')) {
  194. unlink('/etc/openvpn/keys/user.crt');
  195. }
  196. } else {
  197. file_put_contents('/etc/openvpn/keys/user.crt', $config['crt_client']);
  198. }
  199. if(empty($config['crt_client_key'])) {
  200. if(file_exists('/etc/openvpn/keys/user.key')) {
  201. unlink('/etc/openvpn/keys/user.key');
  202. }
  203. } else {
  204. file_put_contents('/etc/openvpn/keys/user.key', $config['crt_client_key']);
  205. }
  206. if(empty($config['crt_client_ta'])) {
  207. if(file_exists('/etc/openvpn/keys/user_ta.key')) {
  208. unlink('/etc/openvpn/keys/user_ta.key');
  209. }
  210. } else {
  211. file_put_contents('/etc/openvpn/keys/user_ta.key', $config['crt_client_ta']);
  212. }
  213. if(empty($config['crt_server_ca'])) {
  214. if(file_exists('/etc/openvpn/keys/ca-server.crt')) {
  215. unlink('/etc/openvpn/keys/ca-server.crt');
  216. }
  217. } else {
  218. file_put_contents('/etc/openvpn/keys/ca-server.crt', $config['crt_server_ca']);
  219. }
  220. } else {
  221. file_put_contents('/etc/openvpn/client.conf.tpl', $_POST['raw_openvpn']);
  222. if($_FILES['crt_client']['error'] == UPLOAD_ERR_OK) {
  223. move_uploaded_file($_FILES['crt_client']['tmp_name'], '/etc/openvpn/keys/user.crt');
  224. } elseif($_POST['crt_client_delete'] == 1) {
  225. unlink('/etc/openvpn/keys/user.crt');
  226. }
  227. if($_FILES['crt_client_key']['error'] == UPLOAD_ERR_OK) {
  228. move_uploaded_file($_FILES['crt_client_key']['tmp_name'], '/etc/openvpn/keys/user.key');
  229. } elseif($_POST['crt_client_key_delete'] == 1) {
  230. unlink('/etc/openvpn/keys/user.key');
  231. }
  232. if($_FILES['crt_client_ta']['error'] == UPLOAD_ERR_OK) {
  233. move_uploaded_file($_FILES['crt_client_ta']['tmp_name'], '/etc/openvpn/keys/user_ta.key');
  234. } elseif($_POST['crt_client_ta_delete'] == 1) {
  235. unlink('/etc/openvpn/keys/user_ta.key');
  236. }
  237. if($_FILES['crt_server_ca']['error'] == UPLOAD_ERR_OK) {
  238. move_uploaded_file($_FILES['crt_server_ca']['tmp_name'], '/etc/openvpn/keys/ca-server.crt');
  239. }
  240. }
  241. if(!empty($config['login_user'])) {
  242. file_put_contents('/etc/openvpn/keys/credentials', "${config['login_user']}\n${config['login_passphrase']}");
  243. } else {
  244. file_put_contents('/etc/openvpn/keys/credentials', '');
  245. }
  246. $retcode = start_service();
  247. if($retcode == 0) {
  248. flash('success', _('Configuration updated and service successfully reloaded'));
  249. } else {
  250. flash('error', _('Configuration updated but service reload failed'));
  251. }
  252. } else {
  253. flash('success', _('Service successfully disabled'));
  254. }
  255. redirect:
  256. redirect_to('/');
  257. });
  258. dispatch('/status', function() {
  259. $status_lines = service_status();
  260. $status_list = '';
  261. foreach($status_lines AS $status_line) {
  262. if(preg_match('/^\[INFO\]/', $status_line)) {
  263. $status_list .= '<li class="status-info">'.htmlspecialchars($status_line).'</li>';
  264. }
  265. elseif(preg_match('/^\[OK\]/', $status_line)) {
  266. $status_list .= '<li class="status-success">'.htmlspecialchars($status_line).'</li>';
  267. }
  268. elseif(preg_match('/^\[WARN\]/', $status_line)) {
  269. $status_list .= '<li class="status-warning">'.htmlspecialchars($status_line).'</li>';
  270. }
  271. elseif(preg_match('/^\[ERR\]/', $status_line)) {
  272. $status_list .= '<li class="status-danger">'.htmlspecialchars($status_line).'</li>';
  273. }
  274. }
  275. echo $status_list;
  276. });
  277. dispatch('/lang/:locale', function($locale = 'en') {
  278. switch($locale) {
  279. case 'fr':
  280. $_SESSION['locale'] = 'fr';
  281. break;
  282. default:
  283. $_SESSION['locale'] = 'en';
  284. }
  285. redirect_to('/');
  286. });