Security.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * HSDN PHP Whois Server Daemon
  4. *
  5. * @author HSDN Team
  6. * @copyright (c) 2015, Information Networks Ltd.
  7. * @link http://www.hsdn.org
  8. */
  9. namespace pWhoisd;
  10. use pWhoisd\Application;
  11. use pWhoisd\Inet;
  12. /**
  13. * Security class.
  14. */
  15. class Security {
  16. /*
  17. * @const int One second period
  18. */
  19. const interval_second = 1;
  20. /*
  21. * @const int One minute period
  22. */
  23. const interval_minute = 60;
  24. /*
  25. * @const int One hour period
  26. */
  27. const interval_hour = 3600;
  28. /*
  29. * @const int One day period
  30. */
  31. const interval_day = 86400;
  32. /*
  33. * @var array Interval pools [[interval, time(), key, counter], ...]
  34. */
  35. private $interval_pool = [];
  36. /*
  37. * @var object Instance of Client class
  38. */
  39. private $client;
  40. /*
  41. * @var array Access control rules
  42. */
  43. private $rules;
  44. /*
  45. * @var string Message sent to client
  46. */
  47. private $message;
  48. /*
  49. * @var string Last assigned action
  50. */
  51. private $action;
  52. /**
  53. * Assigning class properties and parse rules configuration.
  54. *
  55. * @return void
  56. */
  57. public function __construct()
  58. {
  59. $this->rules = Application::$config->get('security.rules');
  60. $this->parse_rules();
  61. }
  62. /**
  63. * Initialize security by client request.
  64. *
  65. * @param object $client Instance of Client class
  66. * @return void
  67. */
  68. public function initialize(Client $client)
  69. {
  70. $this->client = $client;
  71. $this->process_rules();
  72. }
  73. /**
  74. * Process defined access control rules.
  75. *
  76. * @return void
  77. */
  78. private function process_rules()
  79. {
  80. $this->message = NULL;
  81. $this->action = NULL;
  82. $this->intervals = [];
  83. foreach (array_reverse($this->rules) as $rule)
  84. {
  85. if ($this->process_conditions($rule['conditions']))
  86. {
  87. $this->message = Application::$config->get('messages.'.$rule['message'], FALSE);
  88. $this->action = $rule['action'];
  89. }
  90. }
  91. // Update presented intervals
  92. foreach ($this->intervals as $key => $interval)
  93. {
  94. $this->interval_check($interval, $key, TRUE);
  95. }
  96. }
  97. /**
  98. * Process rule condition.
  99. *
  100. * @return bool TRUE if all rules is accepted
  101. */
  102. private function process_conditions($conditions)
  103. {
  104. if (empty($conditions))
  105. {
  106. return TRUE;
  107. }
  108. $client_address = $this->client->get_address();
  109. $expressions = [TRUE];
  110. foreach (['client_ip', 'requests', 'rate'] as $matched_condition)
  111. {
  112. foreach ($conditions as $condition)
  113. {
  114. $expression = NULL;
  115. if ($condition['l'] != $matched_condition)
  116. {
  117. continue;
  118. }
  119. switch ($condition['l'])
  120. {
  121. // Match client IP policy
  122. case 'client_ip':
  123. $expression = $this->compare_client_ip($condition['r']);
  124. break;
  125. // Client-based interval policy
  126. case 'requests':
  127. $expression = $this->compare_interval
  128. (
  129. $condition['op'],
  130. $condition['r'],
  131. $client_address
  132. );
  133. break;
  134. // Global interval policy
  135. case 'rate':
  136. $expression = $this->compare_interval
  137. (
  138. $condition['op'],
  139. $condition['r']
  140. );
  141. break;
  142. }
  143. if ($expression !== NULL)
  144. {
  145. $expressions[] = $expression;
  146. if ($expression === FALSE)
  147. {
  148. break 2; // stop conditions checking
  149. }
  150. }
  151. }
  152. }
  153. return (bool) min($expressions);
  154. }
  155. /**
  156. * Compares interval method.
  157. *
  158. * @param string Compare operator
  159. * @param string Variable parameter (count/type)
  160. * @param string Interval key
  161. * @return bool TRUE if condition is accepted
  162. */
  163. private function compare_interval($op, $variable, $key = NULL)
  164. {
  165. @list($variable, $interval) = explode('/', $variable, 2);
  166. switch ($interval)
  167. {
  168. case 'sec': $seconds = self::interval_second; break;
  169. case 'min': $seconds = self::interval_minute; break;
  170. case 'hr' : $seconds = self::interval_hour; break;
  171. case 'day': $seconds = self::interval_day; break;
  172. }
  173. if (!isset($seconds) OR empty($variable))
  174. {
  175. return FALSE;
  176. }
  177. $counter = $this->interval_check($seconds, $key);
  178. $this->intervals[$key] = $seconds;
  179. $variable = (float) $variable;
  180. switch ($op)
  181. {
  182. case '==': return $counter == $variable;
  183. case '!=': return $counter != $variable;
  184. case '>=': return $counter >= $variable;
  185. case '<=': return $counter <= $variable;
  186. case '>' : return $counter > $variable;
  187. case '<' : return $counter < $variable;
  188. }
  189. return FALSE;
  190. }
  191. /**
  192. * Compares IP address method.
  193. *
  194. * @param string|array Variable parameter
  195. * @return bool TRUE if condition is accepted
  196. */
  197. private function compare_client_ip($variable)
  198. {
  199. return Inet::ip_in_subnets($this->client->get_address(), $variable);
  200. }
  201. /**
  202. * Checks all intervals and returns current counter value.
  203. *
  204. * @param int Interval type
  205. * @param string Interval key
  206. * @return int
  207. */
  208. private function interval_check($interval = self::interval_second, $key = NULL, $update = FALSE)
  209. {
  210. $counter = FALSE;
  211. foreach ($this->interval_pool as $id => $element)
  212. {
  213. if ($element[1] <= time() - $element[0])
  214. {
  215. $this->interval_pool[$id] = NULL;
  216. unset($this->interval_pool[$id]);
  217. continue;
  218. }
  219. if ($element[0] == $interval AND $element[2] == $key)
  220. {
  221. if ($update)
  222. {
  223. $counter = ++$this->interval_pool[$id][3];
  224. }
  225. else
  226. {
  227. $counter = $element[3];
  228. }
  229. }
  230. }
  231. if ($counter === FALSE)
  232. {
  233. $counter = 1;
  234. if ($update)
  235. {
  236. $this->interval_pool[] = [$interval, time(), $key, ++$counter];
  237. }
  238. }
  239. return $counter;
  240. }
  241. /**
  242. * Parse access control rules configuration.
  243. *
  244. * @return void
  245. */
  246. private function parse_rules()
  247. {
  248. $rules = [];
  249. foreach ($this->rules as $id => $rule)
  250. {
  251. if (!is_array($rule))
  252. {
  253. continue;
  254. }
  255. $rules[$id]['action'] = NULL;
  256. $rules[$id]['message'] = NULL;
  257. $rules[$id]['conditions'] = [];
  258. foreach ($rule as $rule_part)
  259. {
  260. // Assigning action
  261. if (is_null($rules[$id]['action']) AND ($rule_part == 'allow' OR $rule_part == 'deny' OR $rule_part == 'drop'))
  262. {
  263. $rules[$id]['action'] = $rule_part;
  264. }
  265. // Assigning message
  266. elseif (is_null($rules[$id]['message']) AND is_string($rule_part))
  267. {
  268. $rules[$id]['message'] = $rule_part;
  269. }
  270. // Assugning conditions
  271. elseif (is_array($rule_part) AND sizeof($rule_part) >= 2)
  272. {
  273. $rules[$id]['conditions'][] = $this->parse_condition($rule_part);
  274. }
  275. }
  276. if (is_null($rules[$id]['action']))
  277. {
  278. unset($rules[$id]);
  279. }
  280. }
  281. $this->rules = $rules;
  282. }
  283. /**
  284. * Parse conditions in rule configuration.
  285. *
  286. * @param array Raw conditions array
  287. * @return array
  288. */
  289. private function parse_condition($condition_part)
  290. {
  291. $condition = [];
  292. if (sizeof($condition_part) >= 3)
  293. {
  294. $condition['l'] = $condition_part[0];
  295. $condition['op'] = $condition_part[1];
  296. $condition['r'] = $condition_part[2];
  297. }
  298. else
  299. {
  300. $condition['l'] = $condition_part[0];
  301. $condition['r'] = $condition_part[1];
  302. }
  303. if (!isset($condition['op']) OR !in_array($condition['op'], ['==', '!=', '=<', '=>', '<', '>']))
  304. {
  305. $condition['op'] = '==';
  306. }
  307. return $condition;
  308. }
  309. /**
  310. * Gets message sent to client.
  311. *
  312. * @return string|NULL
  313. */
  314. public function get_message()
  315. {
  316. return $this->message;
  317. }
  318. /**
  319. * Gets last assigned action.
  320. *
  321. * @return string
  322. */
  323. public function get_action()
  324. {
  325. return strtolower($this->action);
  326. }
  327. } // end of class Security