firewall.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. require_once('includes/lib_remote.php');
  25. require_once("includes/form_table_class.php");
  26. function exec_ogp_module() {
  27. global $view;
  28. global $db;
  29. $rhost_id = @$_REQUEST['rhost_id'];
  30. $remote_server = $db->getRemoteServer($rhost_id);
  31. if ( $remote_server === FALSE )
  32. {
  33. print_failure(get_lang_f('invalid_remote_host_id',$rhost_id));
  34. $view->refresh("?m=server");
  35. return;
  36. }
  37. echo "<h2>".get_lang_f('editing_firewall_for_remote_server',$remote_server['remote_server_name'])."</h2>";
  38. $firewall_settings = $db->getFirewallSettings($remote_server['remote_server_id']);
  39. $remote = new OGPRemoteLibrary($remote_server['agent_ip'],$remote_server['agent_port'],$remote_server['encryption_key'],$remote_server['timeout']);
  40. $host_stat = $remote->status_chk();
  41. if( isset($_POST['reset_firewall']) )
  42. {
  43. if($host_stat === 1)
  44. {
  45. $remote->sudo_exec($firewall_settings['disable_firewall_command']);
  46. $remote->sudo_exec($firewall_settings['reset_firewall_command']);
  47. $addresses = explode(",",$firewall_settings['default_allowed']);
  48. if(is_array($addresses))
  49. {
  50. $trimed_addresses = array();
  51. foreach($addresses as $key => $address)
  52. {
  53. $address = trim($address);
  54. if(strpos($address,':'))
  55. {
  56. list($ip,$port) = explode(':',$address);
  57. set_firewall($remote, $firewall_settings, 'allow', $port, $ip);
  58. }
  59. else
  60. {
  61. $port = trim($address);
  62. set_firewall($remote, $firewall_settings, 'allow', $port);
  63. }
  64. }
  65. }
  66. $remote->sudo_exec($firewall_settings['enable_firewall_command']);
  67. }
  68. }
  69. if( isset($_POST['save_firewall_settings']) )
  70. {
  71. $addresses = explode(",",$_POST['default_allowed']);
  72. if(is_array($addresses))
  73. {
  74. $trimed_addresses = array();
  75. foreach($addresses as $key => $address)
  76. {
  77. $address = trim($address);
  78. if(strpos($address,':'))
  79. {
  80. list($ip,$port) = explode(':',$address);
  81. if(preg_match('/^[0-9]{1,5}$/',$port)
  82. and $port >= 1 and $port <= 65535
  83. and filter_var($ip, FILTER_VALIDATE_IP)
  84. and !in_array($address,$trimed_addresses))
  85. $trimed_addresses[$key] = $address;
  86. }
  87. else
  88. {
  89. $port = trim($address);
  90. if(preg_match('/^[0-9]{1,5}$/',$port)
  91. and $port >= 1
  92. and $port <= 65535 and
  93. !in_array($port,$trimed_addresses) )
  94. $trimed_addresses[$key] = $port;
  95. }
  96. }
  97. $firewall_settings['default_allowed'] = implode(",",$trimed_addresses);
  98. }
  99. $firewall_settings['allow_port_command'] = trim($_POST['allow_port_command']);
  100. $firewall_settings['deny_port_command'] = trim($_POST['deny_port_command']);
  101. $firewall_settings['allow_ip_port_command'] = trim($_POST['allow_ip_port_command']);
  102. $firewall_settings['deny_ip_port_command'] = trim($_POST['deny_ip_port_command']);
  103. $firewall_settings['enable_firewall_command'] = trim($_POST['enable_firewall_command']);
  104. $firewall_settings['disable_firewall_command'] = trim($_POST['disable_firewall_command']);
  105. $firewall_settings['get_firewall_status_command'] = trim($_POST['get_firewall_status_command']);
  106. $firewall_settings['reset_firewall_command'] = trim($_POST['reset_firewall_command']);
  107. $db->updateFirewallSettings($remote_server['remote_server_id'],$firewall_settings);
  108. }
  109. if( isset($_GET['ch_fw_status']) )
  110. {
  111. $firewall_settings['status'] = $_GET['ch_fw_status'];
  112. if($host_stat === 1)
  113. {
  114. if($_GET['ch_fw_status'] == "enable")
  115. {
  116. $addresses = explode(",",$firewall_settings['default_allowed']);
  117. if(is_array($addresses))
  118. {
  119. $trimed_addresses = array();
  120. foreach($addresses as $key => $address)
  121. {
  122. $address = trim($address);
  123. if(strpos($address,':'))
  124. {
  125. list($ip,$port) = explode(':',$address);
  126. set_firewall($remote, $firewall_settings, 'allow', $port, $ip);
  127. }
  128. else
  129. {
  130. $port = trim($address);
  131. set_firewall($remote, $firewall_settings, 'allow', $port);
  132. }
  133. }
  134. }
  135. $remote->sudo_exec($firewall_settings['enable_firewall_command']);
  136. }
  137. else
  138. {
  139. $remote->sudo_exec($firewall_settings['disable_firewall_command']);
  140. }
  141. }
  142. if($db->updateFirewallSettings($remote_server['remote_server_id'],$firewall_settings))
  143. {
  144. $firewall_settings = $db->getFirewallSettings($remote_server['remote_server_id']);
  145. }
  146. }
  147. if($firewall_settings['status'] == "enable")
  148. {
  149. echo "<b>".get_lang('status')."</b> ".get_lang('on')."<br />
  150. <a href='?m=server&amp;p=firewall&amp;rhost_id=".
  151. $remote_server['remote_server_id']."&amp;ch_fw_status=disable'>[".get_lang('stop_firewall')."]</a>\n";
  152. }
  153. else
  154. {
  155. echo "<b>".get_lang('status')."</b> ".get_lang('off')."<br />
  156. <a href='?m=server&amp;p=firewall&amp;rhost_id=".
  157. $remote_server['remote_server_id']."&amp;ch_fw_status=enable'>[".get_lang('start_firewall')."]</a>\n";
  158. }
  159. $ft = new FormTable();
  160. $ft->start_form("?m=server&amp;p=firewall&amp;rhost_id=$rhost_id");
  161. $ft->start_table();
  162. $ft->add_field('text','default_allowed',$firewall_settings['default_allowed'], 38);
  163. $ft->add_field('string','allow_port_command',$firewall_settings['allow_port_command']);
  164. $ft->add_field('string','deny_port_command',$firewall_settings['deny_port_command']);
  165. $ft->add_field('string','allow_ip_port_command',$firewall_settings['allow_ip_port_command']);
  166. $ft->add_field('string','deny_ip_port_command',$firewall_settings['deny_ip_port_command']);
  167. $ft->add_field('string','enable_firewall_command',$firewall_settings['enable_firewall_command']);
  168. $ft->add_field('string','disable_firewall_command',$firewall_settings['disable_firewall_command']);
  169. $ft->add_field('string','get_firewall_status_command',$firewall_settings['get_firewall_status_command']);
  170. $ft->add_field('string','reset_firewall_command',$firewall_settings['reset_firewall_command']);
  171. $ft->end_table();
  172. $ft->add_button('submit','save_firewall_settings',get_lang("save_firewall_settings"));
  173. $ft->end_form();
  174. echo "<h3>".get_lang("firewall_status")."</h3>";
  175. echo "<pre class='log'>";
  176. echo $remote->sudo_exec($firewall_settings['get_firewall_status_command']);
  177. echo "</pre>";
  178. $ft = new FormTable();
  179. $ft->start_form("?m=server&amp;p=firewall&amp;rhost_id=$rhost_id");
  180. $ft->add_button('submit','reset_firewall',get_lang("reset_firewall"));
  181. $ft->end_form();
  182. echo create_back_button($_GET['m']);
  183. }