add_dhcp.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/qauth.php");
  3. if (!empty($_GET["ip"]) and !empty($_GET["mac"])) {
  4. $ip = $_GET["ip"];
  5. $mac = mac_dotted(trim($_GET["mac"]));
  6. $dhcp_hostname = '';
  7. if (!empty($_GET["hostname"])) { $dhcp_hostname = trim($_GET["hostname"]); }
  8. $faction = $_GET["action"] * 1;
  9. $action = 'add';
  10. if ($faction == 1) { $action = 'add'; }
  11. if ($faction == 0) { $action = 'del'; }
  12. LOG_VERBOSE($db_link, "external dhcp request for $ip [$mac] $action");
  13. if (checkValidIp($ip) and is_our_network($db_link, $ip)) {
  14. $log_dhcp = 1;
  15. $ip_aton = ip2long($ip);
  16. //check hotspot
  17. $hotspot_user = is_hotspot($db_link,$ip);
  18. if ($hotspot_user) {
  19. LOG_DEBUG($db_link,"Hotspot user found!");
  20. $log_dhcp_hotspot = get_option($db_link,44);
  21. if (!isset($log_dhcp_hotspot)) { $log_dhcp_hotspot = 0; }
  22. $log_dhcp = !$log_dhcp_hotspot;
  23. }
  24. $auth = get_record_sql($db_link,"SELECT * FROM User_auth WHERE ip_int=" . $ip_aton . " AND deleted=0");
  25. $aid = NULL;
  26. if (!empty($auth)) {
  27. $aid = $auth['id'];
  28. LOG_VERBOSE($db_link,"Found auth for dhcp id: $aid with ip: $ip mac: $mac",$aid);
  29. } else {
  30. LOG_VERBOSE($db_link,"User ip record not found for ip: $ip mac: $mac action: $action. Create it!",0);
  31. $aid = resurrection_auth($db_link, $ip, $mac, $action, $dhcp_hostname);
  32. if (empty($aid)) {
  33. LOG_ERRROR($db_link,"Failed create new user record",0);
  34. exit;
  35. }
  36. LOG_VERBOSE($db_link,"Add user by dhcp request ip: $ip mac: $mac action: $action",$aid);
  37. $auth = get_record_sql($db_link,"SELECT * FROM User_auth WHERE id=" . $aid);
  38. }
  39. if ($action ==='del' and !empty($auth['dhcp_time'])) {
  40. $last_time = strtotime($auth['dhcp_time']);
  41. LOG_VERBOSE($db_link,"Delete action found for ip $ip (id: $aid, userid: ".$auth['user_id']."). Last timestamp = ".strftime('%Y-%m-%d %H-%M-%S',$last_time)." Now = ".strftime('%Y-%m-%d %H-%M-%S',time()),$aid);
  42. if ((time() - $last_time>60) and ($auth['ou_id'] == get_const('default_user_ou_id') or $auth['ou_id'] == get_const('default_hotspot_ou_id'))) {
  43. LOG_VERBOSE($db_link,"Remove dynamic user ip (id: $aid) by dhcp request for ip: $ip mac: $mac",$aid);
  44. delete_record($db_link,"User_auth","id=".$aid);
  45. $u_count=get_count_records($db_link,'User_auth','deleted=0 and user_id='.$auth['user_id']);
  46. if ($u_count == 0) {
  47. delete_record($db_link,"User_list","id=".$auth['user_id']);
  48. LOG_VERBOSE($db_link,"Remove dynamic user id: ".$auth['user_id']." by dhcp request",$aid);
  49. }
  50. }
  51. }
  52. if ($log_dhcp) {
  53. $dhcp_log['auth_id'] = $aid;
  54. $dhcp_log['ip'] = $ip;
  55. $dhcp_log['ip_int'] = $ip_aton;
  56. $dhcp_log['mac'] = $mac;
  57. $dhcp_log['action'] = $action;
  58. $dhcp_log['dhcp_hostname'] = $dhcp_hostname;
  59. insert_record($db_link, "dhcp_log", $dhcp_log);
  60. }
  61. } else { LOG_ERROR($db_link, "$ip - wrong network!"); }
  62. }
  63. unset($_GET);
  64. logout();
  65. ?>