authentication.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /**
  3. * authentication
  4. *
  5. * @method validate uses authentication plugins to check if a user is authorized to login
  6. * @method get_domain used to get the domain name from the URL or username and then sets both domain_name and domain_uuid
  7. */
  8. class authentication {
  9. /**
  10. * Define variables and their scope
  11. */
  12. public $domain_uuid;
  13. public $domain_name;
  14. public $username;
  15. public $password;
  16. /**
  17. * Called when the object is created
  18. */
  19. public function __construct() {
  20. }
  21. /**
  22. * validate uses authentication plugins to check if a user is authorized to login
  23. * @return array [plugin] => last plugin used to authenticate the user [authorized] => true or false
  24. */
  25. public function validate() {
  26. //get the domain_name and domain_uuid
  27. if (!isset($this->domain_name) || !isset($this->domain_uuid)) {
  28. $this->get_domain();
  29. }
  30. //start the session if its not started
  31. if (session_status() === PHP_SESSION_NONE) {
  32. session_start();
  33. }
  34. //set the default authentication method to the database
  35. if (!is_array($_SESSION['authentication']['methods'])) {
  36. $_SESSION['authentication']['methods'][] = 'database';
  37. }
  38. //automatically block multiple authentication failures
  39. if (!isset($_SESSION['users']['max_retry']['numeric'])) {
  40. $_SESSION['users']['max_retry']['numeric'] = 5;
  41. }
  42. if (!isset($_SESSION['users']['find_time']['numeric'])) {
  43. $_SESSION['users']['find_time']['numeric'] = 3600;
  44. }
  45. $sql = "select count(user_log_uuid) \n";
  46. $sql .= "from v_user_logs \n";
  47. $sql .= "where result = 'failure' \n";
  48. $sql .= "and floor(extract(epoch from now()) - extract(epoch from timestamp)) < :find_time \n";
  49. $sql .= "and type = 'login' \n";
  50. $sql .= "and remote_address = :remote_address \n";
  51. $sql .= "and username = :username \n";
  52. $parameters['remote_address'] = $_SERVER['REMOTE_ADDR'];
  53. $parameters['find_time'] = $_SESSION['users']['find_time']['numeric'];
  54. $parameters['username'] = $_SESSION['username'];
  55. $database = new database;
  56. $auth_tries = $database->select($sql, $parameters, 'column');
  57. if ($_SESSION['users']['max_retry']['numeric'] <= $auth_tries) {
  58. $result["plugin"] = "database";
  59. $result["domain_name"] = $this->domain_name;
  60. $result["username"] = $this->username;
  61. $result["domain_uuid"] = $this->domain_uuid;
  62. $result["authorized"] = "false";
  63. return $result;
  64. }
  65. //set the database as the default plugin
  66. if (!isset($_SESSION['authentication']['methods'])) {
  67. $_SESSION['authentication']['methods'][] = 'database';
  68. }
  69. //use the authentication plugins
  70. foreach ($_SESSION['authentication']['methods'] as $name) {
  71. //already processed the plugin move to the next plugin
  72. if ($_SESSION['authentication']['plugin'][$name]['authorized']) {
  73. continue;
  74. }
  75. //prepare variables
  76. $class_name = "plugin_".$name;
  77. $base = realpath(dirname(__FILE__)) . "/plugins";
  78. $plugin = $base."/".$name.".php";
  79. //process the plugin
  80. if (file_exists($plugin)) {
  81. include_once $plugin;
  82. $object = new $class_name();
  83. $object->debug = $this->debug;
  84. $object->domain_name = $this->domain_name;
  85. $object->domain_uuid = $this->domain_uuid;
  86. if ($plugin == 'database' && isset($this->key)) {
  87. $object->key = $this->key;
  88. }
  89. if ($plugin == 'database' && isset($this->username)) {
  90. $object->username = $this->username;
  91. $object->password = $this->password;
  92. }
  93. $array = $object->$name();
  94. $id = $array["plugin"];
  95. $result['plugin'] = $array["plugin"];
  96. $result['domain_name'] = $array["domain_name"];
  97. $result['username'] = $array["username"];
  98. $result['user_uuid'] = $array["user_uuid"];
  99. $result['contact_uuid'] = $array["contact_uuid"];
  100. $result['domain_uuid'] = $array["domain_uuid"];
  101. $result['authorized'] = $array["authorized"];
  102. //save the result to the authentication plugin
  103. $_SESSION['authentication']['plugin'][$name] = $result;
  104. }
  105. }
  106. //make sure all plugins are in the array
  107. foreach ($_SESSION['authentication']['methods'] as $name) {
  108. if (!isset($_SESSION['authentication']['plugin'][$name]['authorized'])) {
  109. $_SESSION['authentication']['plugin'][$name]['plugin'] = $name;
  110. $_SESSION['authentication']['plugin'][$name]['domain_name'] = $_SESSION['domain_name'];
  111. $_SESSION['authentication']['plugin'][$name]['domain_uuid'] = $_SESSION['domain_uuid'];
  112. $_SESSION['authentication']['plugin'][$name]['username'] = $_SESSION['username'];
  113. $_SESSION['authentication']['plugin'][$name]['user_uuid'] = $_SESSION['user_uuid'];
  114. $_SESSION['authentication']['plugin'][$name]['authorized'] = 0;
  115. }
  116. }
  117. //debug information
  118. //view_array($_SESSION['authentication'], false);
  119. //set authorized to false if any authentication method failed
  120. $authorized = false;
  121. if (is_array($_SESSION['authentication']['plugin'])) {
  122. foreach($_SESSION['authentication']['plugin'] as $row) {
  123. if ($row["authorized"]) {
  124. $authorized = true;
  125. }
  126. else {
  127. $authorized = false;
  128. break;
  129. }
  130. }
  131. }
  132. //result array
  133. $result["plugin"] = "database";
  134. $result["domain_name"] = $_SESSION['domain_name'];
  135. if (!isset($_SESSION['username'])) {
  136. $result["username"] = $_SESSION['username'];
  137. }
  138. if (!isset($_SESSION['user_uuid'])) {
  139. $result["user_uuid"] = $_SESSION['user_uuid'];
  140. }
  141. $result["domain_uuid"] = $_SESSION['domain_uuid'];
  142. if (!isset($_SESSION['contact_uuid'])) {
  143. $result["contact_uuid"] = $_SESSION['contact_uuid'];
  144. }
  145. $result["authorized"] = $authorized;
  146. //add user logs
  147. if ($result["authorized"]) {
  148. user_logs::add($result);
  149. }
  150. //debug information
  151. //if ($row["authorized"]) {
  152. // echo "authorized: true\n";
  153. //}
  154. //else {
  155. // echo "authorized: false\n";
  156. //}
  157. //user is authorized - get user settings, check user cidr
  158. if ($authorized) {
  159. //set a session variable to indicate authorized is set to true
  160. $_SESSION['authorized'] = true;
  161. //add the username to the session //username seesion could be set soone when check_auth uses an authorized session variable instead
  162. $_SESSION['username'] = $result["username"];
  163. //get the user settings
  164. $sql = "select * from v_user_settings ";
  165. $sql .= "where domain_uuid = :domain_uuid ";
  166. $sql .= "and user_uuid = :user_uuid ";
  167. $sql .= "and user_setting_enabled = 'true' ";
  168. $parameters['domain_uuid'] = $result["domain_uuid"];
  169. $parameters['user_uuid'] = $result["user_uuid"];
  170. $database = new database;
  171. $user_settings = $database->select($sql, $parameters, 'all');
  172. unset($sql, $parameters);
  173. //build the user cidr array
  174. if (is_array($user_settings) && @sizeof($user_settings) != 0) {
  175. foreach ($user_settings as $row) {
  176. if ($row['user_setting_category'] == "domain" && $row['user_setting_subcategory'] == "cidr" && $row['user_setting_name'] == "array") {
  177. $cidr_array[] = $row['user_setting_value'];
  178. }
  179. }
  180. }
  181. //check to see if user address is in the cidr array
  182. if (isset($cidr_array) && !defined('STDIN')) {
  183. $found = false;
  184. foreach($cidr_array as $cidr) {
  185. if (check_cidr($cidr, $_SERVER['REMOTE_ADDR'])) {
  186. $found = true;
  187. break;
  188. }
  189. }
  190. if (!$found) {
  191. //destroy session
  192. session_unset();
  193. session_destroy();
  194. //send http 403
  195. header('HTTP/1.0 403 Forbidden', true, 403);
  196. //exit the code
  197. exit();
  198. }
  199. }
  200. //set the session variables
  201. $_SESSION["domain_uuid"] = $result["domain_uuid"];
  202. //$_SESSION["domain_name"] = $result["domain_name"];
  203. $_SESSION["user_uuid"] = $result["user_uuid"];
  204. $_SESSION["context"] = $result['domain_name'];
  205. //user session array
  206. $_SESSION["user"]["domain_uuid"] = $result["domain_uuid"];
  207. $_SESSION["user"]["domain_name"] = $result["domain_name"];
  208. $_SESSION["user"]["user_uuid"] = $result["user_uuid"];
  209. $_SESSION["user"]["username"] = $result["username"];
  210. $_SESSION["user"]["contact_uuid"] = $result["contact_uuid"];
  211. //get the groups assigned to the user and then set the groups in $_SESSION["groups"]
  212. $sql = "select ";
  213. $sql .= "u.user_group_uuid, ";
  214. $sql .= "u.domain_uuid, ";
  215. $sql .= "u.user_uuid, ";
  216. $sql .= "u.group_uuid, ";
  217. $sql .= "g.group_name, ";
  218. $sql .= "g.group_level ";
  219. $sql .= "from ";
  220. $sql .= "v_user_groups as u, ";
  221. $sql .= "v_groups as g ";
  222. $sql .= "where u.domain_uuid = :domain_uuid ";
  223. $sql .= "and u.user_uuid = :user_uuid ";
  224. $sql .= "and u.group_uuid = g.group_uuid ";
  225. $parameters['domain_uuid'] = $_SESSION["domain_uuid"];
  226. $parameters['user_uuid'] = $_SESSION["user_uuid"];
  227. $database = new database;
  228. $result = $database->select($sql, $parameters, 'all');
  229. $_SESSION["groups"] = $result;
  230. $_SESSION["user"]["groups"] = $result;
  231. unset($sql, $parameters);
  232. //get the users group level
  233. $_SESSION["user"]["group_level"] = 0;
  234. foreach ($_SESSION['user']['groups'] as $row) {
  235. if ($_SESSION["user"]["group_level"] < $row['group_level']) {
  236. $_SESSION["user"]["group_level"] = $row['group_level'];
  237. }
  238. }
  239. //get the permissions assigned to the groups that the user is a member of set the permissions in $_SESSION['permissions']
  240. if (is_array($_SESSION["groups"]) && @sizeof($_SESSION["groups"]) != 0) {
  241. $x = 0;
  242. $sql = "select distinct(permission_name) from v_group_permissions ";
  243. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  244. foreach ($_SESSION["groups"] as $field) {
  245. if (!empty($field['group_name'])) {
  246. $sql_where_or[] = "group_name = :group_name_".$x;
  247. $parameters['group_name_'.$x] = $field['group_name'];
  248. $x++;
  249. }
  250. }
  251. if (is_array($sql_where_or) && @sizeof($sql_where_or) != 0) {
  252. $sql .= "and (".implode(' or ', $sql_where_or).") ";
  253. }
  254. $sql .= "and permission_assigned = 'true' ";
  255. $parameters['domain_uuid'] = $_SESSION["domain_uuid"];
  256. $database = new database;
  257. $result = $database->select($sql, $parameters, 'all');
  258. if (is_array($result) && @sizeof($result) != 0) {
  259. foreach ($result as $row) {
  260. $_SESSION['permissions'][$row["permission_name"]] = true;
  261. $_SESSION["user"]["permissions"][$row["permission_name"]] = true;
  262. }
  263. }
  264. unset($sql, $parameters, $result, $row);
  265. }
  266. //get the domains
  267. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/domains/app_config.php") && !is_cli()){
  268. require_once "app/domains/resources/domains.php";
  269. }
  270. //get the user settings
  271. if (is_array($user_settings) && @sizeof($user_settings) != 0) {
  272. foreach ($user_settings as $row) {
  273. $name = $row['user_setting_name'];
  274. $category = $row['user_setting_category'];
  275. $subcategory = $row['user_setting_subcategory'];
  276. if (!empty($row['user_setting_value'])) {
  277. if (empty($subcategory)) {
  278. //$$category[$name] = $row['domain_setting_value'];
  279. if ($name == "array") {
  280. $_SESSION[$category][] = $row['user_setting_value'];
  281. }
  282. else {
  283. $_SESSION[$category][$name] = $row['user_setting_value'];
  284. }
  285. }
  286. else {
  287. //$$category[$subcategory][$name] = $row['domain_setting_value'];
  288. if ($name == "array") {
  289. $_SESSION[$category][$subcategory][] = $row['user_setting_value'];
  290. }
  291. else {
  292. $_SESSION[$category][$subcategory][$name] = $row['user_setting_value'];
  293. }
  294. }
  295. }
  296. }
  297. }
  298. unset($user_settings);
  299. //get the extensions that are assigned to this user
  300. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/extensions/app_config.php")) {
  301. if (isset($_SESSION["user"]) && is_uuid($_SESSION["user_uuid"]) && is_uuid($_SESSION["domain_uuid"]) && !isset($_SESSION['user']['extension'])) {
  302. //get the user extension list
  303. $_SESSION['user']['extension'] = null;
  304. $sql = "select ";
  305. $sql .= "e.extension_uuid, ";
  306. $sql .= "e.extension, ";
  307. $sql .= "e.number_alias, ";
  308. $sql .= "e.user_context, ";
  309. $sql .= "e.outbound_caller_id_name, ";
  310. $sql .= "e.outbound_caller_id_number, ";
  311. $sql .= "e.description ";
  312. $sql .= "from ";
  313. $sql .= "v_extension_users as u, ";
  314. $sql .= "v_extensions as e ";
  315. $sql .= "where ";
  316. $sql .= "e.domain_uuid = :domain_uuid ";
  317. $sql .= "and e.extension_uuid = u.extension_uuid ";
  318. $sql .= "and u.user_uuid = :user_uuid ";
  319. $sql .= "and e.enabled = 'true' ";
  320. $sql .= "order by ";
  321. $sql .= "e.extension asc ";
  322. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  323. $parameters['user_uuid'] = $_SESSION['user_uuid'];
  324. $database = new database;
  325. $result = $database->select($sql, $parameters, 'all');
  326. if (is_array($result) && @sizeof($result) != 0) {
  327. foreach($result as $x => $row) {
  328. //set the destination
  329. $destination = $row['extension'];
  330. if (!empty($row['number_alias'])) {
  331. $destination = $row['number_alias'];
  332. }
  333. //build the user array
  334. $_SESSION['user']['extension'][$x]['user'] = $row['extension'];
  335. $_SESSION['user']['extension'][$x]['number_alias'] = $row['number_alias'];
  336. $_SESSION['user']['extension'][$x]['destination'] = $destination;
  337. $_SESSION['user']['extension'][$x]['extension_uuid'] = $row['extension_uuid'];
  338. $_SESSION['user']['extension'][$x]['outbound_caller_id_name'] = $row['outbound_caller_id_name'];
  339. $_SESSION['user']['extension'][$x]['outbound_caller_id_number'] = $row['outbound_caller_id_number'];
  340. $_SESSION['user']['extension'][$x]['user_context'] = $row['user_context'];
  341. $_SESSION['user']['extension'][$x]['description'] = $row['description'];
  342. //set the context
  343. $_SESSION['user']['user_context'] = $row["user_context"];
  344. $_SESSION['user_context'] = $row["user_context"];
  345. }
  346. }
  347. unset($sql, $parameters, $result, $row);
  348. }
  349. }
  350. //set the time zone
  351. if (!isset($_SESSION["time_zone"]["user"])) { $_SESSION["time_zone"]["user"] = null; }
  352. if (strlen($_SESSION["time_zone"]["user"] ?? '') === 0) {
  353. //set the domain time zone as the default time zone
  354. date_default_timezone_set($_SESSION['domain']['time_zone']['name']);
  355. }
  356. else {
  357. //set the user defined time zone
  358. date_default_timezone_set($_SESSION["time_zone"]["user"]);
  359. }
  360. } //authorized true
  361. //return the result
  362. return $result;
  363. }
  364. /**
  365. * get_domain used to get the domain name from the URL or username and then sets both domain_name and domain_uuid
  366. */
  367. function get_domain() {
  368. //get the domain from the url
  369. $this->domain_name = $_SERVER["HTTP_HOST"];
  370. //get the domain name from the username
  371. if ($_SESSION["users"]["unique"]["text"] != "global") {
  372. $username_array = explode("@", $_REQUEST["username"] ?? '');
  373. if (count($username_array) > 1) {
  374. //get the domain name
  375. $domain_name = $username_array[count($username_array) -1];
  376. //check if the domain from the username exists then set the domain_uuid
  377. $domain_exists = false;
  378. foreach ($_SESSION['domains'] as $row) {
  379. if (lower_case($row['domain_name']) == lower_case($domain_name)) {
  380. $this->domain_uuid = $row['domain_uuid'];
  381. $domain_exists = true;
  382. break;
  383. }
  384. }
  385. //if the domain exists then set domain_name and update the username
  386. if ($domain_exists) {
  387. $this->domain_name = $domain_name;
  388. $this->username = substr($_REQUEST["username"], 0, -(strlen($domain_name)+1));
  389. $_SESSION['username'] = $this->username;
  390. $_SESSION['domain_uuid'] = $this->domain_uuid;
  391. }
  392. //unset the domain name variable
  393. unset($domain_name);
  394. }
  395. }
  396. //get the domain name from the http value
  397. if (!empty($_REQUEST["domain_name"] ?? '')) {
  398. $this->domain_name = $_REQUEST["domain_name"];
  399. }
  400. //remote port number from the domain name
  401. $domain_array = explode(":", $this->domain_name);
  402. if (count($domain_array) > 1) {
  403. $this->domain_name = $domain_array[0];
  404. }
  405. //get the domain uuid and domain settings
  406. if (isset($this->domain_name) && !isset($this->domain_uuid)) {
  407. foreach ($_SESSION['domains'] as $row) {
  408. if (lower_case($row['domain_name']) == lower_case($this->domain_name)) {
  409. $this->domain_uuid = $row['domain_uuid'];
  410. $_SESSION['domain_uuid'] = $row['domain_uuid'];
  411. break;
  412. }
  413. }
  414. }
  415. //set the setting arrays
  416. $obj = new domains();
  417. $obj->set();
  418. //set the domain settings
  419. $_SESSION['domain_name'] = $this->domain_name;
  420. $_SESSION['domain_parent_uuid'] = $_SESSION["domain_uuid"];
  421. //set the domain name
  422. return $this->domain_name;
  423. }
  424. }
  425. /*
  426. $auth = new authentication;
  427. $auth->username = "user";
  428. $auth->password = "password";
  429. $auth->domain_name = "sip.fusionpbx.com";
  430. $auth->debug = false;
  431. $response = $auth->validate();
  432. print_r($response);
  433. */
  434. ?>